News:

Function Finder  Find all the functions within source code files

Main Menu

Count The Number Of Times a Value Appears Within an Array

Started by kevin, March 27, 2011, 05:34:03 PM

Previous topic - Next topic

kevin

 Count The Number Of Times a Value Appears Within an Array

  This example is really to demo how you the FindArrayCells() function could be used to remove some brute force searching from the PB VM, but not here :).  The demo fills an array of random values (ranged) then counts up the number of times each value appears within the set of data.  To Find each value we're doing a series of linear searches.  You could of course run through and do this manually (shown bellow), but here we're assuming there's some need to use the FIndArrayCells() function.    



 FindArrayCells() Version

PlayBASIC Code: [Select]
   // The range of the values in this array (0-range exclusive)
NumericRange=10

// Size of our array to fill with random values
Size=10000
DIm Array(Size)


// Array to hold the tally of each value
Dim Tally(NumericRange)

Do
Cls
Frames++


// Fill this array full of values
For lp=1 to Size
Array(lp)=floor(Rnd#(NumericRange))
next

// Fill the Tally() array with zeros
ClearArray Tally(),0

t=timer()
// Run through and count the number of times each value
// occurs using FindArrayCell()
For ThisValue=0 to NumericRange-1
StartPos=1
repeat
FoundOffset=FindArrayCell(Array(),StartPos,1,Size,ThisValue)
if FoundOffset>-1
STartPos+=FoundOffset+1
Tally(ThisValue)++
endif
until FoundOffset=-1
next
tt#+=timer()-t

print TT#/frames

// Display the
Total=0
For ThisValue=0 to NumericRange-1
print digits$(ThisValue,2)+" appears "+str$(Tally(ThisValue))+" Times"
Total+=Tally(ThisValue)
next

//
print Total
Sync

while Spacekey()
endwhile
loop






Manual Version

PlayBASIC Code: [Select]
   // The range of the values in this array (0-range exclusive)
NumericRange=10

// Size of our array to fill with random values
Size=10000
DIm Array(Size)


// Array to hold the tally of each value
Dim Tally(NumericRange)

Do
Cls
Frames++


// Fill this array full of values
For lp=1 to Size
Array(lp)=floor(Rnd#(NumericRange))
next

// Fill the Tally() array with zeros
ClearArray Tally(),0

t=timer()
// Run through and count the number of times each value
// occurs by hand
For lp=1 to Size
ThisValue=Array(lp)
Tally(ThisValue)++
next
tt#+=timer()-t

print TT#/frames

// Display the
Total=0
For ThisValue=0 to NumericRange-1
print digits$(ThisValue,2)+" appears "+str$(Tally(ThisValue))+" Times"
Total+=Tally(ThisValue)
next

//
print Total
Sync

while Spacekey()
endwhile
loop






Manual Search Compared to FindArrayCells()

    This is more of direct 'search' comparison between the two methods, no surprises which one wins this race.  


PlayBASIC Code: [Select]
   // Size of our array to fill with random values   
Size=100000
DIm Array(Size)


Do
Cls
Frames++


SearchValue=Mod(SearchValue+1,size)

// Fill this array full of values
if Tick=0
For lp=1 to Size
Array(lp)=lp
next

// Shuffle the array
For lp=1 to Size
Src=rnd(size)
TEmp=Array(lp)
Array(lp)=Array(src)
Array(src)=Temp
next
endif
Tick=Mod(tick+1,10)

t=timer()
Index1=0
// Run through locate this value manually
For lp=1 to Size
if SearchValue=Array(lp)
Index1=lp
exit
endif
next
tt1#+=timer()-t

print "Average Search Time:"+str$((TT1#/frames)*100)


t=timer()
// Use the Find array cell function to brute force search for
// it
Index2=1+FindArrayCell(Array(),1,1,Size,SearchValue)
tt2#+=timer()-t

print "Average Search Time:"+str$((TT2#/frames)*100)

if Index1=Index2
print "Match"

endif




Sync

while Spacekey()
endwhile
loop