Hello friends,
LOCAL lhash := .f.
I have source code here to count how often a customer number appears in a list.
I first tried to solve the problem with ascan. However, it is extremely slow. Maybe I have a logical error and it could be accelerated.
Then I tried with a hash.
Using hashes can significantly speed up operations that involve frequent lookups, such as counting occurrences or maintaining mappings.
In my case, the speed difference is 352 ms compared to 42953 ms.
Best regards,
Otto
LOCAL lhash := .f.
I have source code here to count how often a customer number appears in a list.
I first tried to solve the problem with ascan. However, it is extremely slow. Maybe I have a logical error and it could be accelerated.
Then I tried with a hash.
Using hashes can significantly speed up operations that involve frequent lookups, such as counting occurrences or maintaining mappings.
In my case, the speed difference is 352 ms compared to 42953 ms.
Best regards,
Otto
static function CreateSummaryDbf()
 Â
  LOCAL cDbfPath := "x:\xwhdaten\datawin\BELEGUNG.dbf"
  local hbookings := {=>}
  local I := 0
  local aTmp := ""
  LOCAL aBookings := {}
  LOCAL nKdnr
  LOCAL nCount
  LOCAL lhash := .f. Â
 Â
  use (cDbfPath ) new
  nStartTime := SECONDS()
  do while .not. eof()
   Â
    if lhash = .t.
      IF HHasKey(hbookings, BELEGUNG->GastNR)
        aTmp := hbookings[ BELEGUNG->GastNR ]
        aTmp[1]  := aTmp[1] + 1
        hbookings[ BELEGUNG->GastNR ] := aTmp
      ELSE
        hbookings[ BELEGUNG->GastNR ] := {  1, "test" }
      ENDIF
     Â
    ELSE
      I := AScan( abookings, { |a| a[ 1 ] == BELEGUNG->GastNR } )
     Â
      if I = 0
        AADD( abookings, { BELEGUNG->GastNR, 1, recno()  } )
      else
        abookings[I,2 ]  := abookings[I,2 ] + 1
      endif
    ENDIF
   Â
   Â
    skip
   Â
  enddo
  Â
  if lhash = .t.
    // Convert hash to array for XBROWSE
    FOR EACH nKdnr IN HGetKeys(hBookings)
      AADD(aBookings, { nKdnr, hBookings[nKdnr][1],  hBookings[nKdnr][2]})
    NEXT
  ENDIF
 Â
  nDuration := (SECONDS() - nStartTime) * 1000 // Dauer in Millisekunden
  // Browse the collected data
  xBrowse(aBookings,  "  Duration: " + STR(nDuration, 10, 2) )
 Â
return
//----------------------------------------------------------------------------//