micro-optimization
Cost of exception handlers in Python
Why don’t you measure it using the timeit module? That way you can see whether it’s relevant to your application. OK, so I’ve just tried the following: import timeit statements=[“””\ try: b = 10/a except ZeroDivisionError: pass”””, “””\ if a: b = 10/a”””, “b = 10/a”] for a in (1,0): for s in statements: t … 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
Should I use Java’s String.format() if performance is important?
I took hhafez code and added a memory test: private static void test() { Runtime runtime = Runtime.getRuntime(); long memory; … memory = runtime.freeMemory(); // for loop code memory = memory-runtime.freeMemory(); I run this separately for each approach, the ‘+’ operator, String.format and StringBuilder (calling toString()), so the memory used will not be affected by … Read more