Thanks for the explanations, so in this example if I use the for / loop to read bytes from a big file, it would be much faster ?
Yes. There the I/O could be a bottleneck, but you should still see some kind of improvement. So you should declare types whenever you can. The general purpose "var" is very big (can be any size - from 1byte to n bytes, but maybe Josh could say more precisely) and slow (I guess because the compiler cannot easily optimize it like it would basic data types).
I thought that was the usual way of using FOR with int, it's even documented that way in GM.
Where in the GM documentation? I think you missed the point on what I did there. I changed "var" into "int". GM doesn't allow that. It only has "var". I could of changed it into "double" or "char" or "size_t" or anything else if I wanted to. But those types are what makes it so fast.
Actually you want unsigned int, because it is never negative, or size_t.
I personally think using unsigned variables are becoming a bad practice quite rapidly. They generate A LOT of errors, especially for newbies, and so they are not encouraged. They were required when you actually needed the extra "size" (like when int was only 65536), but now when int's are +-2,147,483,647 which is usually "enough". Soon it will be better to just use double everywhere. Like if you wanted the loop to work in reverse:
int counter;
counter = 0;
for (unsigned int i=1000000; i>=0; --i){
counter+=1;
}
This is would end up as infinite loop. And most newbies wouldn't even figure out why. I guess this is why there are no unsigned types in Java. Speed wise unsigned and signed is more complex. unsigned can be optimized in some interesting ways (like
http://stackoverflow.com/a/12225693) but they can be slower to cast to float (
http://plog.sesse.net/blog/tech/2011-07-27-10-37_the_micro_optimization_corner_unsigned_to_float.html).
But I guess if you make a forward going loop you should use size_t, as it usually also gives you a good indication on what the loop is doing (that it accesses arrays usually).
Another reason is because you would have to explicitly cast unsigned to signed for comparison. Like if you run ENIGMA the first time (when engine is compiled) the majority of warnings (and there are A LOT of them.. we should probably get on that) is because we compare signed and unsigned variables. If we used signed everywhere we wouldn't have that problem and yet the code would usually run the same (as we don't really go over 2.1 billion mark in resources, instance count, text glyphs (even with Unicode ranges) and so on).
edit:
for (int counter=0; counter<1000000; ++counter)
You could. The reason why I didn't is because an empty for loop sometimes could be just optimized out. Read this:
http://punchlet.wordpress.com/2011/07/01/letter-the-eighth-bedevilling-benchmarks/So while it probably wouldn't do it in Run mode (which isn't optimized) it maybe would do it Compile mode. So for illustrating a point I made it so the loop wouldn't be optimized out.