Hello,
I would like to share my findings into how to find an item in a linked list, in this case a certain string. This is part of a much larger program, but simple to understand. In my app i have a list of known spam adresses (12000 of them) and want to find if the email i recieve is from a spammer.
Look at this code run for yourself and see the difference. One warning do NOT try with more than 10000 since the 'wrong' for-next loop takes ages to run.
Notice i have had to 'slow down' the right way 520 times!!
Here is my code contribution:
Code: Select all
EnableExplicit
NewList slTest.s()
Define Seek.s
Define X.l
Define Count.l
Define Idx.l
Define T1.l, T2.l
Seek.s = "xxtest"
For x = 1 To 10000
AddElement( slTest.s())
slTest.s() = "test+" + Str( x)
Next
; this is index number 10000 and not 10001 since we start from ZERO!!
AddElement( slTest())
slTest() = Seek.s
OpenConsole()
; Try to find the "xxtest" in the linkedlist and how long it takes, version 1 for-next loop...
PrintN( "=Start = " + Seek.s)
Idx. l = -1
T1.l = ElapsedMilliseconds()
Count.l = ListSize( slTest.s())
For Idx.l = 0 To Count.l
SelectElement( slTest.s(), Idx.l)
If slTest.s() = Seek.s
Break
EndIf
Next
T2.l = ElapsedMilliseconds()
If Idx.l > -1
PrintN( "=Found in: " + Str( T2 - T1) + "ms")
PrintN( "At index: "+ Str( Idx.l))
EndIf
; Now the ForEach methode. It is SOO fast had to slow it down 520 times..
Idx. l = -1
T1.l = ElapsedMilliseconds()
For x = 1 To 520
ForEach slTest.s()
If slTest.s() = Seek.s
Idx.l = ListIndex( slTest.s())
Break
EndIf
Next
Next
T2.l = ElapsedMilliseconds()
If Idx.l > -1
PrintN( "=Found in: " + Str( T2 - T1) + "ms")
PrintN( "At index: "+ Str( Idx.l))
Else
PrintN( "!!NOT!! Found in: " + Str( T2 - T1) + "ms")
EndIf
Input()
CloseConsole()
End
Jan V.