Is there a penalty for calling millisecs()?
BlitzMax Forums/BlitzMax Beginners Area/Is there a penalty for calling millisecs()?
| ||
In the past I've tended to store system calls like millisecs() graphicswidth() etc in variables whereever possible to avoid calling them repeatedly. Just wondering if it's actually advantageous or not. |
| ||
Using a variable will always be faster then doing a function call, however, in some cases, it wouldn't be very useful. GraphicsWidth() probably won't change on you, and if it does you can update the Variable at that time. Millsecs() changes every... oh, every 1/1000 of a second :-) So you will probably need to keep calling that function. If the value changes A LOT then you're better off calling the function. If the value rarely changes but you access it a lot, then you should use a variable. If the value rarely changes and you rarely access it, then you could do either one. If the value changes a lot, but you need to have a value from a given point in time, then you will need to do both. Call the function, save it in a variable then use the variable. You have to look at every situation and make the determination of what is best for your situation. |
| ||
I'll keep putting Millisecs() in a variable then. It may change every 1/1000th of a second, but there are a number of times when I do batches of timed events where the time is not going to have changed and wouldn't matter even if it did. |
| ||
graphicswidth() is a really fast command. Just not so fast to type! I tested it Graphics 800,600 Const am=10000000 For do=1 To 2 Delay 500 time=MilliSecs() For l=1 To am aoksdgmnaorigm=GraphicsWidth() Next took=MilliSecs()-time Print "function: "+took Delay 500 time=MilliSecs() holder=GraphicsWidth() For l=1 To am lsgmaprgmsgrgsrf=holder Next took=MilliSecs()-time Print "variable: "+took Next |
| ||
Using a variable will always be faster then doing a function call, however, in some cases, it wouldn't be very useful. Not nessecarilly. Reading from RAM (aka. variables) is the #1 bottleneck in a modern PC. For starters, RAM is slow, and secondly, the CPU cannot do anything else until it gets the value it needs.If you have a function that can be done entirely with the CPU (although there aren't many on x86 processors with only 4 registers) or can fit within the on-die cache, it will be faster than hitting memory. However these days it doesn't matter. At all. Unless you have something that takes hundreds of thousands of cycles, good enough is best. |