On Thursday, 24 January 2013 at 10:21:06 UTC, Walter Bright wrote:
On 1/23/2013 9:10 PM, Era Scarecrow wrote:
I remember doing things like that. If I was dividing
something by 8 I would
shift right instead by 3;
Compilers were doing that optimization 35 years ago, and
probably decades longer than that.
Generally, if you're thinking about doing an optimization, it
pays to check the output of the compiler, as it has probably
beaten you to it :-)
Well, some micro optimization pay off, but any straightforward
one is already done by the compiler. Even some tricky one in fact.
One that is not done to the best of my knowledge is the dual loop
:
uint countChar(char* cstr, char c) {
uint count;
while(*cstr) {
while(*cstr && *cstr != c) cstr++;
if(*cstr == c) {
count++;
cstr++;
}
}
}
Which is faster as the straightforward way of doing things.