micro-optimization
Latency bounds and throughput bounds for processors for operations that must occur in sequence
Terminology: you can say a loop is “bound on latency”, but when analyzing that bottleneck I wouldn’t say “the latency bound” or “bounds”. That sounds wrong to me. The thing you’re measuring (or calculating via static performance analysis) is the latency or length of the critical path, or the length of the loop-carried dependency chain. … Read more
What is the fastest way to find if a number is even or odd?
It is pretty well known that static inline int is_odd_A(int x) { return x & 1; } is more efficient than static inline int is_odd_B(int x) { return x % 2; } But with the optimizer on, will is_odd_B be no different from is_odd_A? No — with gcc-4.2 -O2, we get, (in ARM assembly): _is_odd_A: … Read more
When, if ever, is loop unrolling still useful?
Loop unrolling makes sense if you can break dependency chains. This gives a out of order or super-scalar CPU the possibility to schedule things better and thus run faster. A simple example: for (int i=0; i<n; i++) { sum += data[i]; } Here the dependency chain of the arguments is very short. If you get … Read more
Is reading the `length` property of an array really that expensive an operation in JavaScript?
Well, I would have said it was expensive, but then I wrote a little test @ jsperf.com and to my surprise using i<array.length actually was faster in Chrome, and in FF(4) it didn’t matter. My suspicion is that length is stored as an integer (Uint32). From the ECMA-specs (262 ed. 5, page 121): Every Array … Read more
what is faster: in_array or isset? [closed]
The answers so far are spot-on. Using isset in this case is faster because It uses an O(1) hash search on the key whereas in_array must check every value until it finds a match. Being an opcode, it has less overhead than calling the in_array built-in function. These can be demonstrated by using an array … Read more