Speed testing - for n to x compared to foreach and if elseif else comparison

The place for any Hollywood tutorials
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Speed testing - for n to x compared to foreach and if elseif else comparison

Post by Bugala »

Code: Select all

testtable = {}

For n=1 To 100
	InsertItem(testtable, {name="item"..n, number=Rnd(1)} )
Next




StartTimer(1)
amount = TableItems(testtable)
For n=1 To 10000
	For m = 0 To amount-1
		item = testtable[m]
		If item.number = 1 Then item.number=2
		If item.number = 0 Then item.number=2
	Next
Next

time = GetTimer(1)
DebugPrint(time)



ResetTimer(1)
amount = TableItems(testtable)
For n=1 To 10000
	For m = 0 To amount-1
		If testtable[m].number = 1 Then testtable[m].number=2
		If testtable[m].number = 2 Then testtable[m].number=2
	Next
Next

time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To 10000
	ForEach(testtable, Function (ID, item)
		If item.number = 1 Then item.number=2
		If item.number = 2 Then item.number=2
			EndFunction)
Next

time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To 10000
	ForEach(testtable, Function (ID, item)
		If item.number = 1 
			item.number=2
		ElseIf item.number = 2
			item.number=2
		EndIf
			EndFunction)
Next

time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To 10000
	ForEach(testtable, Function (ID, item)
		If item.number = 1 
			item.number=2
		Else
			item.number=2
		EndIf
			EndFunction)
Next


time = GetTimer(1)
DebugPrint(time)
This results in my machine about:
1360 (for 1 to x - indirect call to a variable inside a table)
1183 (for 1 to x - direct call to a variable inside a table)
1025 (two IFs)
1173 (IF-ELSEIF-ENDIF)
966 (IF-ELSE-ENDIF)


Meaning that FOREACH is clearly faster than FOR n=1 to x

Also it appears IF-ELSE-ENDIF is faster than two IFs or IF-ELSEIF-ENDIF structure.

I was expecting two IFs to be slower than IF-ELSEIF-ENDIF structure, but appears it is faster using two IFs instead.

I might do more speed tests and report about them here.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Speed testing - for n to x compared to foreach and if elseif else comparison

Post by Bugala »

Next test is about several ELSEIFs with different ELSEIF being the answer. Also ELSEIF-ELSE and ELSEIF¨-SWITCH comparison:

Code: Select all

timestodo = 10000000
DebugPrint("IF-ELSEIF-ENDIF")
StartTimer(1)

For a = -1 To 6

   b = 0

   ResetTimer(1)
   For n=1 To timestodo
      If a=1
   	b=1
      ElseIf a=2
	b=1
      ElseIf a=3
	b=1
      ElseIf a=4
	b=1
      ElseIf a=5
	b=1
      EndIf
   Next

   time = GetTimer(1)
   DebugPrint("a:"..a.."  "..time)

Next
DebugPrint("a=100")
 ResetTimer(1)
a=100
   For n=1 To timestodo
      If a=1
   	b=1
      ElseIf a=2
	b=1
      ElseIf a=3
	b=1
      ElseIf a=4
	b=1
      ElseIf a=5
	b=1
      EndIf
   Next

time = GetTimer(1)
   DebugPrint("a:"..a.."  "..time)

DebugPrint("IF-ELSEIF-ELSE-ENDIF")


For a = -1 To 6

   b = 0

   ResetTimer(1)
   For n=1 To timestodo
      If a=1
   	b=1
      ElseIf a=2
	b=1
      ElseIf a=3
	b=1
      ElseIf a=4
	b=1
      ElseIf a=5
	b=1
      Else
	b=1
      EndIf
   Next

   time = GetTimer(1)
   DebugPrint("a:"..a.."  "..time)

Next




DebugPrint("a=100 ELSE")
 ResetTimer(1)
a=100
   For n=1 To timestodo
      If a=1
   	b=1
      ElseIf a=2
	b=1
      ElseIf a=3
	b=1
      ElseIf a=4
	b=1
      ElseIf a=5
	b=1
      Else
	b=1
      EndIf
   Next

time = GetTimer(1)
   DebugPrint("a:"..a.."  "..time)



DebugPrint("SWITCH")


For a = -1 To 6

   b = 0

   ResetTimer(1)
   For n=1 To timestodo
      Switch a
	Case 1:
   	b=1
	Case 2:
	b=1
	Case 3:
	b=1
	Case 4:
	b=1
	Case 5:
	b=1
      EndSwitch
   Next

   time = GetTimer(1)
   DebugPrint("a:"..a.."  "..time)

Next



DebugPrint("SWITCH with Default:")


For a = -1 To 6

   b = 0

   ResetTimer(1)
   For n=1 To timestodo
      Switch a
	Case 1:
   	b=1
	Case 2:
	b=1
	Case 3:
	b=1
	Case 4:
	b=1
	Case 5:
	b=1
	Default:
	b=1
      EndSwitch
   Next

   time = GetTimer(1)
   DebugPrint("a:"..a.."  "..time)

Next
result:

Code: Select all

IF-ELSEIF-ENDIF          IF-ELSEIF-ELSE-ENDIF              SWITCH                  SWITCH with Default
a:-1 18488                        a:-1 21414                       a:-1  24156              a:-1  24559
a:0  18176                        a:0  21354                       a:0  24613                a:0  24931
a:1  10688                        a:1  11205                       a:1  16580                a:1  16479
a:2  13000                        a:2  13569                       a:2  19502                a:2  19634
a:3  16086                        a:3  16570                       a:3  22240                a:3  21947
a:4  18854                        a:4  18606                       a:4  25149               a:4  28785
a:5  22332                        a:5  21406                       a:5  25350               a:5  27359
a:6  18620                        a:6  20944                       a:6  25210               a:6  24656
a:100  18431                    a:100  21592
In previous test, IF-ELSEIF-ELSE-ENDIF structure was faster than without ELSE option, however, this time there don't seem to be much difference in performance. ELSE might seem slower with these numbers, but in ELSE case there is one extra line of "B=1" compared to non ELSE version.

There also don't seem to be a difference in ELSE number being whatever number.

However, when I was trying to make this fewer times, it seemed like 0 was beating the rest, but in fewer times results varied a lot each run time, so can't be trusted.


In addition there was comparison to SWITCH statement. If "default" option existed or not, didnt seem to make much difference. However, it is clear that IF-ELSEIF-ELSE-ENDIF is faster than using switch statements. Also, similar to IF options was that sooner the right option was in code, less time it would take to execute it.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Speed testing - for n to x compared to foreach and if elseif else comparison

Post by Bugala »

HaveItem vs RawGet:

Code: Select all

StartTimer(1)

amountoftimestocheck = 10000000

a = {}
a.b = 1

ResetTimer(1)
For n=1 To amountoftimestocheck

   If RawGet(a, "a")
   Else
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimestocheck

   If RawGet(a, "b")
   Else
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimestocheck

   If HaveItem(a, "a")
   Else
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimestocheck

   If HaveItem(a, "b")
   Else
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)




ResetTimer(1)
For n=1 To amountoftimestocheck

   c=0
   If RawGet(a, "a")
	c=1
   Else
	c=1
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimestocheck

   c=0
   If RawGet(a, "b")
	c=1
   Else
	c=1
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimestocheck

   c=0
   If HaveItem(a, "a")
	c=1
   Else
	c=1
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimestocheck

   c=0
   If HaveItem(a, "b")
	c=1
   Else
	c=1
   EndIf
Next
time = GetTimer(1)
DebugPrint(time)
results in:
8616
8671
9613
9319
13502
13276
14926
14901

Finding or not finding the HaveItem/RawGet dont seem to make any noticeable difference.

However, RawGet is somewhat faster than HaveItem, that the results do show, and bit strangely the difference seems to be bigger when code actually does something.

That in first four numbers where IF-ELSE-ENDIF doesnt actually do anything, the difference is around 1000 Ms between RawGet and HaveItem, yet in the next four numbers where they all do the same of C=0, the difference is around 1500 Ms, which I find interesting.

Anyway, regardless, Rawget is always faster than HaveItem.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Speed testing - for n to x compared to foreach and if elseif else comparison

Post by Bugala »

OR vs multiple IF vs ELSEIF:

Code: Select all

StartTimer(1)
amountoftimestodo = 10000000

For a=-1 To 6
ResetTimer(1)
   For n=1 To amountoftimestodo
	c=0
	If a = 1 Or a=2 Or a=3 Or a=4 Or a=5
	   c=1
	EndIf

   Next
time=GetTimer(1)
DebugPrint("a:"..a.." time:"..time)
Next


For a=-1 To 6
ResetTimer(1)
   For n=1 To amountoftimestodo
	c=0
	If a = 1 Then c=1
	If a = 2 Then c=2
	If a = 3 Then c=3
	If a = 4 Then c=4
	If a = 5 Then c=5
   Next
time=GetTimer(1)
DebugPrint("a:"..a.." time:"..time)
Next


For a=-1 To 6
ResetTimer(1)
   For n=1 To amountoftimestodo
	c=0
	If a = 1
		c=1
	ElseIf a = 2 
		c=2
	ElseIf a = 3
		c=3
	ElseIf a = 4
		c=4
	ElseIf a = 5
		c=5
	EndIf
   Next
time=GetTimer(1)
DebugPrint("a:"..a.." time:"..time)
Next
Results in:

Code: Select all

      OR                                 multiple IF                    ELSEIF
a:-1 time:12261                a:-1 time:17736            a:-1 time:25832
a:0 time:11569                 a:0 time:17772             a:0 time:19460
a:1 time:11912                 a:1 time:18643             a:1 time:12489
a:2 time:12949                 a:2 time:18326             a:2 time:14462
a:3 time:13064                 a:3 time:18610             a:3 time:17336
a:4 time:13392                 a:4 time:17983             a:4 time:19829
a:5 time:14514                 a:5 time:18263             a:5 time:21961
a:6 time:11860                 a:6 time:18015             a:6 time:19544

OR is clearly a faster option than the rest. interestingly Multiple IF and ELSEIF are not always clear which one is faster. Basically, when one of the conditions do happen, ELSEIF is faster, but if none of the conditions happen, then it appears using Multiple IFs is actually faster than ELSEIF structure. However, this needs to be calculated against the speed increase of succesful ones. With Multiple IFs speed is constant, regardless if which condition is true or not, while with ELSEIF earlier the true condition, the faster it executes.

OR also has the same thing, that earlier the right condition, the faster the execution, and it seems that no condition met in OR is about as fast as first OR being right.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Speed testing - for n to x compared to foreach and if elseif else comparison

Post by jalih »

Bugala wrote: Fri Sep 20, 2019 4:04 pm Meaning that FOREACH is clearly faster than FOR n=1 to x
Try using

Code: Select all

for local n=1 to x
and speed is probably about the same...
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Speed testing - for n to x compared to foreach and if elseif else comparison

Post by Bugala »

I never even thought that Global and Local Variables would have difference in speed, so based upon Jalihs comment took a following try:

Code: Select all

testtable = {}

For n=1 To 100
	InsertItem(testtable, {name="item"..n, number=Rnd(1)} )
Next

amountoftimes=1000000


StartTimer(1)


ResetTimer(1)
amount = TableItems(testtable)
For n=1 To amountoftimes
	For Local m = 0 To amount-1
		If testtable[m].number = 1 Then testtable[m].number=2
		If testtable[m].number = 2 Then testtable[m].number=2
	Next
Next

time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimes
	ForEach(testtable, Function (ID, item)
		If item.number = 1 Then item.number=2
		If item.number = 2 Then item.number=2
			EndFunction)
Next

time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimes
	ForEach(testtable, Function (ID, item)
		If item.number = 1 
			item.number=2
		Else
			item.number=2
		EndIf
			EndFunction)
Next


time = GetTimer(1)
DebugPrint(time)


ResetTimer(1)
For n=1 To amountoftimes
	For Local m = 0 To amount-1
		If testtable[m].number = 1
			testtable[m].number=2

		Else
			testtable[m].number=2

		EndIf
	Next
Next


time = GetTimer(1)
DebugPrint(time)
And this resulted in:
96656
104074
100294
95094

Indicating that actually for LOCAL n = 1 to x is a bit faster method than using Foreach.
Post Reply