I am trying to speed up some code, but I'm baffled with the compiler's response. It seems like sometimes when I remove unnecessary code, instead of it running faster, my routines sometimes run slower. And changing code which would appear to be irrelevent tends to affect the program's speed as well.
I have a DLL function that takes two arguments. I figured that by rearranging the code so that it takes only one argument would make it faster, but just the opposite seems to happen. I don't know if this is just on my machine (PIII 600Mhz). So hopefully others will test out the sample code below.
Here are the tests to try. First compile and run the program as given below. Then comment out the line that says: Answer = TestA(1, 1) , and un-comment the one that says: Answer = TestB(1). And compare the speeds. On my machine the one with one argument is remarkably slower than the one with two.
Here's another test. Comment out the first Dim statement, and un-comment the line below it to see the speed difference. Now the 1 arg function runs faster, but why? No code was changed within the loop. I thought that maybe register variables might have something to do with it, as I've had problems with it before, so I added "$Register None", but that apparently wasn't it.
I'm trying to optimize my code, but the above phenomena makes it difficult. Hopefully, PB tech support can explain why this happens, so that I might have an easier time optimizing my code. For now, it seems like I'm kind of poking in the dark in some of my attempts.
One other strange thing. If I remove the "q As Long" from the Dim statement
(q is not even used anywhere), TestA runs much slower.
------------------
Daniel Corbier
UCalc Fast Math Parser
http://www.ucalc.com
I have a DLL function that takes two arguments. I figured that by rearranging the code so that it takes only one argument would make it faster, but just the opposite seems to happen. I don't know if this is just on my machine (PIII 600Mhz). So hopefully others will test out the sample code below.
Here are the tests to try. First compile and run the program as given below. Then comment out the line that says: Answer = TestA(1, 1) , and un-comment the one that says: Answer = TestB(1). And compare the speeds. On my machine the one with one argument is remarkably slower than the one with two.
Here's another test. Comment out the first Dim statement, and un-comment the line below it to see the speed difference. Now the 1 arg function runs faster, but why? No code was changed within the loop. I thought that maybe register variables might have something to do with it, as I've had problems with it before, so I added "$Register None", but that apparently wasn't it.
I'm trying to optimize my code, but the above phenomena makes it difficult. Hopefully, PB tech support can explain why this happens, so that I might have an easier time optimizing my code. For now, it seems like I'm kind of poking in the dark in some of my attempts.
Code:
$Compile Exe "test.exe" DefLng A-Z Function TestA(ByVal Arg1, ByVal Arg2) As Ext TestA = 1 + 1 End Function Function TestB(ByVal Arg1) As Ext TestB = 1 + 1 End Function Function WinMain (ByVal hCurInstance As Long, _ ByVal hPrevInstance As Long, _ lpszCmdLine As Asciiz Ptr, _ ByVal nCmdShow As Long) Export As Long Dim x As Long, y As Ext, z As Long, Answer As Ext, q As Long 'Dim x As Long, Answer As Ext t! = Timer For x = 0 To 100000000 Answer = TestA(1, 1) ' Answer = TestB(1) Next tt! = Timer-t! MsgBox Str$(tt!) End Function
(q is not even used anywhere), TestA runs much slower.
------------------
Daniel Corbier
UCalc Fast Math Parser
http://www.ucalc.com
Comment