Optimisations
Blitz3D Forums/Blitz3D Programming/Optimisations
| ||
I thought it might be good for us to share some of our optimisation tricks. Here's my first contribution to get the ball rolling, in this example I am demonstrating that when using floats whilst it is quicker to add an integer to a float value, when it comes to conditional checking a float like if xPosition#>320 it's actualy faster to type if xPosition#>320.0;Conditional Checking (faster to test a float versus a float) n#=0.0 start=MilliSecs() For n=1 To 10000000 If n>1.0 EndIf Next Print "Testing 1.0 : "+(MilliSecs()-start) n#=0.0 start=MilliSecs() For n=1 To 10000000 If n>1 EndIf Next Print "Testing 1 : "+(MilliSecs()-start) ;Addition Test (faster to add a hard coded integer to a float) n#=0.0 start=MilliSecs() For n=1 To 10000000 n=n+1.0 Next Print "Adding 1.0 : "+(MilliSecs()-start) n#=0.0 start=MilliSecs() For n=1 To 10000000 n=n+1 Next Print "Adding 1 : "+(MilliSecs()-start) ;Addition Test (faster to add a variable integer to a float) n#=0.0 o#=1.0 start=MilliSecs() For n=1 To 10000000 n=n+o Next Print "Adding var#: "+(MilliSecs()-start) n#=0.0 p=1 start=MilliSecs() For n=1 To 10000000 n=n+p Next Print "Adding var : "+(MilliSecs()-start) Does anyone have any other tricks ? |
| ||
when it comes to conditional checking a float like if xPosition#>320 it's actualy faster to type if xPosition#>320.0 How can it be faster to type something with 2 more characters ? ;) |
| ||
Because in runtime, the compiler is not converting the value you typed into a float in order to compare it. I was quite confused as to why the same did not apply for addition. |
| ||
Can you feel something pulling your leg Becky?...yes it was Smiff ;) |
| ||
1. Reduction! Reduce the number of operations, calculations, loop iterations. Sounds like common sense, but, often overlooked. 2. When possible, use Integers vs Stings with conditions: IF...THEN, SELECT...CASE, WHILE, UNTIL. |
| ||
Becky Rose, take care with your tests. You are using the same variable in the sum and in the for loop, so you are doing wrong calculations. EDITED: Sorry, i was checking wrong. My code only shows that it is faster not to use the "for" variables to check things, xDDD This should be the correct code. Faster anyway, but no so much: ;Conditional Checking (faster to test a float versus a float) n#=0.0 start=MilliSecs() For j=1 To 10000000 If n>1.0 EndIf Next Print "Testing 1.0 : "+(MilliSecs()-start) n#=0.0 start=MilliSecs() For j=1 To 10000000 If n>1 EndIf Next Print "Testing 1 : "+(MilliSecs()-start) ;Addition Test (faster to add a hard coded integer to a float) n#=0.0 start=MilliSecs() For j=1 To 10000000 n=n+1.0 Next Print "Adding 1.0 : "+(MilliSecs()-start) n#=0.0 start=MilliSecs() For j=1 To 10000000 n=n+1 Next Print "Adding 1 : "+(MilliSecs()-start) ;Addition Test (faster to add a variable integer to a float) n#=0.0 o#=1.0 start=MilliSecs() For j=1 To 10000000 n=n+o Next Print "Adding var#: "+(MilliSecs()-start) n#=0.0 p=1 start=MilliSecs() For j=1 To 10000000 n=n+p Next Print "Adding var : "+(MilliSecs()-start) |
| ||
oh yeah.... that was a #up! |
| ||
now which is faster, hmm? ten million interations for a 5 millisecond difference means they are the same speed. |
| ||
You need a slower computer Coorrae. The real test of optimisation is not the amount of saving but the consistency. If it's slower/faster every time in a given method then it's worth doing - and in the case of conditional checking and adding values to a variable then it's definately worth doing. I just wonder if the same tasks are faster on all processor families... ? I'm using an AMD, it's a 2200+ I think, or thereabouts. |
| ||
It's hard to say anything based on small differences. Here are the first two tests, each done twice. Running this, with debug off, I get different results for the two tests. The first time the 1.0 test is clearly faster. The second time the 1.0 and 1 results are nearly identical. ;Conditional Checking (faster to test a float versus a float) n#=0.0 start=MilliSecs() For n=1 To 10000000 If n>1.0 EndIf Next Print "Testing 1.0 : "+(MilliSecs()-start) n#=0.0 start=MilliSecs() For n=1 To 10000000 If n>1 EndIf Next Print "Testing 1 : "+(MilliSecs()-start) ;Conditional Checking (faster to test a float versus a float) n#=0.0 start=MilliSecs() For n=1 To 10000000 If n>1.0 EndIf Next Print "Testing 1.0 : "+(MilliSecs()-start) n#=0.0 start=MilliSecs() For n=1 To 10000000 If n>1 EndIf Next Print "Testing 1 : "+(MilliSecs()-start) WaitKey |
| ||
Put a Delat 1000 at the beginning of the code, as blitz sometimes takes a time to inialise. This will make the test fairer. |