... well dwords seem to be quite fast when used with SHIFT LEFT and SHIFT RIGHt. But otherwise, they seem to be very slow.
I've been spending quite a few hours today and yesterday trying to optimize a piece of code which I had translated over from C source. The C source was about roughly 6x as fast as mine and I couldnt figure out why. I was using DWords in every case where they were using "unsigned longs" When compiled, my app was taking ~40 seconds to go from start to finish, the C app was taking ~7 seconds. After converting all my Dwords to Longs, my code is now ~11 seconds which is much better. Im going to make a few more changes and hopefully I can catch or even beat 7 seconds.
Anyone know why the C code wasnt getting hammered when using the "unsigned long" variable (the PB equiv of dword right?) I compiled a version of the C code using "longs" instead of "unsigned longs" and it seemed to actually get slower by about 1.5 seconds... why could that be?
Here are the results to a few tests I was running. If you can give any insight into why some of the numbers are the way they are (some of which seem counter intuitive) please do!
CONCLUSIONS
What I learned (i think)
- Avoid using Singles, Doubles and ESPECIALLY DWords and Words with bitwise operations like AND, OR, and XOR
- Floating points are extremely slow when it comes to addition and substraction
- When bitshifting, Local variables are faster than registered variables. (Anyone know why?)
- EXT Floating points, and Doubles and Singles are slightly faster than other types when it comes to square roots and raising power.
- During multiplication and divisions, register integers, longs and words are slower than local declared integers, longs and words. Anyone know why?
Thanks!
-Mike
[This message has been edited by Mike Joseph (edited April 01, 2000).]
I've been spending quite a few hours today and yesterday trying to optimize a piece of code which I had translated over from C source. The C source was about roughly 6x as fast as mine and I couldnt figure out why. I was using DWords in every case where they were using "unsigned longs" When compiled, my app was taking ~40 seconds to go from start to finish, the C app was taking ~7 seconds. After converting all my Dwords to Longs, my code is now ~11 seconds which is much better. Im going to make a few more changes and hopefully I can catch or even beat 7 seconds.
Anyone know why the C code wasnt getting hammered when using the "unsigned long" variable (the PB equiv of dword right?) I compiled a version of the C code using "longs" instead of "unsigned longs" and it seemed to actually get slower by about 1.5 seconds... why could that be?
Here are the results to a few tests I was running. If you can give any insight into why some of the numbers are the way they are (some of which seem counter intuitive) please do!
Code:
Test Machine = Pentium II Celeron 266 w/ 128megs PC100 RAM, running Windows98 original release. Data types tested = Integer, Long, Word, DWord, Single, Double Declarations Tested = LOCAL, REGISTER, EXT --------------------------------------- --------------------------------------- Test 1: XOR, AND, OR 50 million itterations -------------------------------------- #COMPILE EXE #REGISTER NONE #DIM ALL FUNCTION PBMAIN() REGISTER TestVariable1 AS LONG REGISTER TestVariable2 AS LONG DIM Counter AS LONG DIM lngTime AS LONG DIM Itterations AS LONG Itterations = 50000000 TestVariable1 = 1 TestVariable1 = 2 lngTime = TIMER FOR Counter = 1 TO Itterations TestVariable1 = TestVariable1 XOR TestVariable2 TestVariable2 = TestVariable2 AND TestVariable1 TestVariable1 = TestVariable2 OR TestVariable1 NEXT Counter MSGBOX FORMAT$(Itterations,"#,###,###") & " itterations in " & FORMAT$(TIMER - lngTime,"####.##") & " seconds." END FUNCTION -------- Results | -------- Registered Longs 1.37 seconds Registered Words 1.87 seconds Local Words 3.33 seconds Local Longs 3.84 seconds Local Integers 4.12 seconds Registered Integers 4.81 seconds (Note these were consistantly slower than LOCAL integers EXT Precision Floats 18.49 seconds Local Singles 19.98 seconds Local Doubles 25.14 seconds Registered Dwords 39.25 seconds Local Dwords 43.55 seconds --------------------------------------- --------------------------------------- Test 2: SHIFT LEFT , SHIFT RIGHT 50 million itterations -------------------------------------- #COMPILE EXE #REGISTER NONE #DIM ALL FUNCTION PBMAIN() REGISTER TestVariable1 AS LONG REGISTER TestVariable2 AS LONG DIM Counter AS LONG DIM lngTime AS LONG DIM Itterations AS LONG Itterations = 50000000 TestVariable1 = 16 TestVariable1 = 32 lngTime = TIMER FOR Counter = 1 TO Itterations SHIFT LEFT TestVariable1, 8& SHIFT LEFT TestVariable2, 8& SHIFT RIGHT TestVariable1, 8& SHIFT RIGHT TestVariable2, 8& NEXT Counter MSGBOX FORMAT$(Itterations,"#,###,###") & " itterations in " & FORMAT$(TIMER - lngTime,"####.##") & " seconds." END FUNCTION -------- Results | -------- Local Words 6.89 seconds (~2x faster than registered word) Local Integers 7.07 seconds (locals seem to be faster than their registered counterparts overall) Local Dwords 7.89 seconds Local Longs 7.90 seconds Registered Dwords 8.20 seconds Registered Longs 8.47 seconds Registered Integers 12.20 seconds Registered Words 12.23 seconds EXT Precision Floats N/A Local Singles N/A Local Doubles N/A --------------------------------------------- --------------------------------------------- TEST 3: MULIPLY, DIVIDE 5 million itterations --------------------------------------------- #COMPILE EXE #REGISTER NONE #DIM ALL FUNCTION PBMAIN() REGISTER TestVariable1 AS LONG REGISTER TestVariable2 AS LONG DIM Counter AS LONG DIM lngTime AS LONG DIM Itterations AS LONG Itterations = 5000000 TestVariable1 = 100 TestVariable2 = 200 lngTime = TIMER FOR Counter = 1 TO Itterations TestVariable1 = TestVariable1 * TestVariable2 TestVariable2 = TestVariable2 * TestVariable1 TestVariable1 = TestVariable1 / TestVariable2 TestVariable2 = TestVariable2 / TestVariable1 NEXT Counter MSGBOX FORMAT$(Itterations,"#,###,###") & " itterations in " & FORMAT$(TIMER - lngTime,"####.##") & " seconds." END FUNCTION -------- Results | -------- Local Longs 5.26 seconds Local Integers 5.29 seconds Local Singles 9.06 seconds Registered Longs 9.44 seconds (why are these slower than Local declared longs?) EXT Precision Floats 9.66 seconds Registered Words 9.88 seconds Registered Integers 9.88 seconds (same as registered Words) Local Doubles 10.65 seconds Local Words 10.71 seconds Registered DWords 12.13 seconds Local DWords 13.22 seconds --------------------------------------------- --------------------------------------------- TEST 4: ADDITIONS, SUBTRACTION, INCR 50 million itterations --------------------------------------------- #COMPILE EXE #REGISTER NONE #DIM ALL FUNCTION PBMAIN() REGISTER TestVariable1 AS LONG REGISTER TestVariable2 AS LONG DIM Counter AS LONG DIM lngTime AS LONG DIM Itterations AS LONG Itterations = 50000000 TestVariable1 = 999 TestVariable2 = 111 lngTime = TIMER FOR Counter = 1 TO Itterations TestVariable1 = TestVariable1 + TestVariable1 TestVariable2 = TestVariable2 + TestVariable2 TestVariable1 = TestVariable1 - TestVariable2 TestVariable2 = TestVariable1 - TestVariable2 NEXT Counter MSGBOX FORMAT$(Itterations,"#,###,###") & " itterations in " & FORMAT$(TIMER - lngTime,"####.##") & " seconds." END FUNCTION -------- Results | -------- Registered Longs 1.34 seconds Registered Words 2.74 seconds Registered DWordS 2.96 seconds Local Integers 3.74 seconds Local Words 3.92 seconds Local Longs 3.96 seconds Local DWords 4.17 seconds Registered Integers 6.18 seconds (slower than Local Integers, and significantly slower than registered Words) EXT Precision Floats 91.99 seconds Local Singles 92.34 seconds Local Doubles 101.05 seconds --------------------------------------------- --------------------------------------------- TEST 5: SQUARE ROOTS and EXPONENTS 50 million itterations --------------------------------------------- #COMPILE EXE #REGISTER NONE #DIM ALL FUNCTION PBMAIN() REGISTER TestVariable1 AS LONG REGISTER TestVariable2 AS LONG DIM Counter AS LONG DIM lngTime AS LONG DIM Itterations AS LONG Itterations = 50000000 TestVariable1 = 2 TestVariable2 = 3 lngTime = TIMER FOR Counter = 1 TO Itterations TestVariable1 = TestVariable1^1 TestVariable2 = TestVariable2^1 TestVariable1 = sqr(TestVariable2) TestVariable2 = sqr(TestVariable1) NEXT Counter MSGBOX FORMAT$(Itterations,"#,###,###") & " itterations in " & FORMAT$(TIMER - lngTime,"####.##") & " seconds." END FUNCTION -------- Results | -------- EXT Precision Floats 38.94 seconds EXT registered precision floating points top in this scenario Local Singles 39.91 seconds Local Longs 43.85 seconds Local Integers 43.75 seconds Local Doubles 45.27 seconds Registered Words 46.20 seconds Registered Integers 46.40 seconds Registered Longs 46.41 seconds Registered DWORDS 53.37 seconds Local Words 55.92 seconds Local DWORDS 60.02 seconds -----------------------------------------------------
What I learned (i think)
- Avoid using Singles, Doubles and ESPECIALLY DWords and Words with bitwise operations like AND, OR, and XOR
- Floating points are extremely slow when it comes to addition and substraction
- When bitshifting, Local variables are faster than registered variables. (Anyone know why?)
- EXT Floating points, and Doubles and Singles are slightly faster than other types when it comes to square roots and raising power.
- During multiplication and divisions, register integers, longs and words are slower than local declared integers, longs and words. Anyone know why?
Thanks!
-Mike
[This message has been edited by Mike Joseph (edited April 01, 2000).]
Comment