optimization
Why is Swift compile time so slow?
Well, it turned out that Rob Napier was right. It was one single file (actually one method) that was causing the compiler to go berzek. Now don’t get me wrong. Swift does recompile all your files each time, but the great thing now, is that Apple added real-time compilation feedback over the files it compiles, … Read more
Ternary operators in JavaScript without an “else”
First of all, a ternary expression is not a replacement for an if/else construct – it’s an equivalent to an if/else construct that returns a value. That is, an if/else clause is code, a ternary expression is an expression, meaning that it returns a value. This means several things: use ternary expressions only when you … Read more
Where to place JavaScript in an HTML file?
The Yahoo! Exceptional Performance team recommend placing scripts at the bottom of your page because of the way browsers download components. Of course Levi’s comment “just before you need it and no sooner” is really the correct answer, i.e. “it depends”.
How to log a method’s execution time exactly in milliseconds?
NSDate *methodStart = [NSDate date]; /* … Do whatever you need to do … */ NSDate *methodFinish = [NSDate date]; NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart]; NSLog(@”executionTime = %f”, executionTime); Swift: let methodStart = NSDate() /* … Do whatever you need to do … */ let methodFinish = NSDate() let executionTime = methodFinish.timeIntervalSinceDate(methodStart) print(“Execution time: \(executionTime)”) … Read more
Why is transposing a matrix of 512×512 much slower than transposing a matrix of 513×513?
The explanation comes from Agner Fog in Optimizing software in C++ and it reduces to how data is accessed and stored in the cache. For terms and detailed info, see the wiki entry on caching, I’m gonna narrow it down here. A cache is organized in sets and lines. At a time, only one set … Read more
Rounding up to next power of 2
Check the Bit Twiddling Hacks. You need to get the base 2 logarithm, then add 1 to that. Example for a 32-bit value: Round up to the next highest power of 2 unsigned int v; // compute the next highest power of 2 of 32-bit v v–; v |= v >> 1; v |= v … Read more
Fastest way to convert string to integer in PHP
I’ve just set up a quick benchmarking exercise: Function time to run 1 million iterations ——————————————– (int) “123”: 0.55029 intval(“123”): 1.0115 (183%) (int) “0”: 0.42461 intval(“0”): 0.95683 (225%) (int) int: 0.1502 intval(int): 0.65716 (438%) (int) array(“a”, “b”): 0.91264 intval(array(“a”, “b”)): 1.47681 (162%) (int) “hello”: 0.42208 intval(“hello”): 0.93678 (222%) On average, calling intval() is two and … Read more
Declaring variables inside or outside of a loop
The scope of local variables should always be the smallest possible. In your example I presume str is not used outside of the while loop, otherwise you would not be asking the question, because declaring it inside the while loop would not be an option, since it would not compile. So, since str is not … Read more