Hi,
I don' t even need to benchmark the speed of local vars vs global vars. Sure in this example where its only 1 global variable and only decreasing it. But its a whole other thing if you have 5 vars in a loop where they all are set to a differnt value with each iteration.
When using loops its not just for while %x dec %x...there can be many vars involved, many vars set to different values for each iteration. Of course I don' t need to tell you this, as far as programming goes, you're most likely x number of times better than me (I actually rarely script with the exception of the RPG bot i made, and some little snippets to help people). Imo using global vars instead of local vars in a loop slows scripts down noticeably without even benchmarking.
Note that I said:
I wouldnt be so crazy to use a global variable to loop. I have probably given the impression that i was referring to the iteration variable, but i simply mean the usage of global variables vs local variables where global variables arent needed, in performing a loop.
Here's for example the alias that i use in my rpg bot:
alias addstats {
; Usage: /addstats nick 1 20 3 40 ... OR /addstats nick = 1=20=5=son goku=14=...
; Reason for second option: text which contains spaces
var %nick = $h.enc($1)
var %stats = $hget(stats,%nick)
var %locate, %value, %f.value, %l.value
var %chr = $iif($2 == =,61,32)
var %line = $iif($2 == =,$3-,$2-)
var %x = $numtok(%line,%chr)
tokenize 61 %stats
while (%x) {
%locate = $gettok(%line,$calc(%x - 1),%chr)
%l.value = $gettok(%line,%x,%chr)
%f.value = [ [ $+($,%locate) ] ]
if (%locate isnum 2-13) || (%locate == 20) || (%l.value == DEAD) || (%f.value == DEAD) { %value = %l.value | goto next }
else %value = $calc(%f.value + %l.value)
if (%value < 0) %value = 0
elseif (%locate == 16) && (%value > 100) %value = 100
elseif (%locate == 17) && (%value > 400) %value = 400
elseif (%locate == 1) %value = $zeropad(%value)
:next
%stats = $puttok(%stats,%value,%locate,61)
dec %x 2
}
hadd stats %nick %stats
}
Now change all the local vars to global vars, and then benchmark it.
Note that I really don't want to begin any sort of flame in the global vs local vars.
Matter of fact, I'll let you have the last word in this thread, as I won't reply.
Greetz
Edit: since being on these mirc msg boards, I more than ever realize that its very important to express oneself in the clearest way. Any words or sentence constructions used in a way that can be interpreted in different ways, will most likely always be interpreted in the other ways than intended. I'll have to be very careful with this, as I think that is what happened here. Nevertheless, I think it's fun helping people, and even going into discussion wiht other people (as long as its civilized), as it is good learning experience and well..earth would be a rather boring place if everyone thought the same thing.