Better way to declare in loops
Hey guys, I've been hearing a lot that I should declare variable outside the loop for example:
PHP Code:
PHP Code:
|
Re: Better way to declare in loops
Good practice to assume it doesn't.
|
Re: Better way to declare in loops
You could compile it and then run it through the lysis online decompiler. Spcomp also has an -a flag that you might be interested in.
|
Re: Better way to declare in loops
Nice idea, I'll try that tomorrow, Good night for now.
|
Re: Better way to declare in loops
AFAIK, the variable is "allocated" at compile time (on stack or heap), thus both code should give the same binary.
|
Re: Better way to declare in loops
Quote:
|
Re: Better way to declare in loops
Declaring the variable outside the "for" block:
PHP Code:
PHP Code:
|
Re: Better way to declare in loops
It doesn't look like the variable even made it into the code in the second snippet at all...
|
Re: Better way to declare in loops
Quote:
|
Re: Better way to declare in loops
They probably do optimize that (can't be bothered to check the C++ source) since the JIT does a more or less 1-to-1 mapping of SM bytecode to assembly/machine code.
It's better to assume that the compiler is dumb and does a simple 1-to-1 mapping of pawn to bytecode with no optimizations in place. Having said that, you should only declare things outside of loop if the loop is to be ran an insane number of times (insane being relative, of course). If the loop is only ran several times (on the order of magnitude of 100), depending on complexity it's sometimes better to just leave the declaration in the loop. Otherwise you may introduce subtle bugs in your plugin just for a 0.0000001% speedup or something (premature optimization trap). Realistically speaking though, integer declarations are pretty much negligible. It's only sizable string or array declarations that you should be looking at moving outside of loops or to global scope. |
| All times are GMT -4. The time now is 08:50. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.