Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualHodgman

Posted 15 June 2013 - 09:56 AM

Getting off topic now, but FWIW, on many CPUs, "literal << variable" is far, far slower than "variable << literal".
The former (lhs<<rhs where rhs isn't known at compile time) is often microcoded and/or implemented as the loop: output=lhs;for(int i=0; i!=rhs; ++i) output <<= 1;
i.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
for(i=0; ...; ++i) { mask = 1<<i; ... }
Becomes:
for(i=0, mask=1; ...; ++i, mask<<=1) { ... }

However, the OP code might benefit from not repeating this expensive operation (or operator[]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png

#4Hodgman

Posted 15 June 2013 - 09:44 AM

Getting off topic now, but FWIW, on most CPUs, "literal << variable" is far, far slower than "variable << literal".

The former (lhs<<rhs where rhs isn't known at compile time) is often microcoded and/or implemented as the loop: output=lhs;for(int i=0; i!=rhs; ++i) output <<= 1;
i.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
for(i=0; ...; ++i) { mask = 1<<i; ... }
Becomes:
for(i=0, mask=1; ...; ++i, mask<<=1) { ... }

However, the OP code might benefit from not repeating this expensive operation (or operator[]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png


#3Hodgman

Posted 15 June 2013 - 09:43 AM

Getting off topic now, but FWIW, on most CPUs, "literal << variable" is far, far slower than "variable << literal".

The latter is often microcoded and/or implemented as the loop: output=lhs;for(int i=0; i!=rhs; ++i) output <<= 1;
i.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
for(i=0; ...; ++i) { mask = 1<<i; ... }
Becomes:
for(i=0, mask=1; ...; ++i, mask<<=1) { ... }

However, the OP code might benefit from not repeating this expensive operation (or operator[]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png


#2Hodgman

Posted 15 June 2013 - 09:36 AM

Getting off topic now, but FWIW, on most CPUs, "literal << variable" is far, far slower than "variable << literal".
I.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
for(i=0; ...; ++i) { mask = 1<<i; ... }
Becomes:
for(i=0, mask=1; ...; ++i, mask<<=1) { ... }

However, the above code might benefit from not repeating this expensive operation (or operator[]) 3 times in a row, and instead caching the result once.

#1Hodgman

Posted 15 June 2013 - 09:33 AM

Getting off topic now, but FWIW, on most CPUs, "literal << variable" is far, far slower than "variable << literal".
I.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
for(i=0; ...; ++i) { mask = 1<<i; ... }
Becomes:
for(i=0, mask=1; ...; ++i, mask<<=1) { ... }

PARTNERS