We need to bear in mind the Ahmdal’s Law when doing code optimisation.
The speed up can be described in the following equation:
Speedup = time_old /time_new
Where, the func_cost (notation c) is the percentage of the program runtime used by the function func, and func_speedup (notation by s) is the factor by which you speedup the function.
For example, if you optimize a function foo(), which is 40% of the runtime, so that it runs as twice as faster, your overall application will run 25% faster based on the above equation:
This means infrequently used code (e.g., the scene preparation) should be optimized little (or avoided if possible). This is often quoted as: “make the common case fast and the rare case correct.”
Ahmdal’s Law by Javascript Implementation
Let’s compute some values and we choose Javascript to implement this equation:
1 2 3 | var ahmdal_law = function(c, s) { return 1.0 / (1 - c + c / s); } |
var ahmdal_law = function(c, s) { return 1.0 / (1 - c + c / s); }
Let’s assume that s = 2 (two times faster) and
1 2 3 | for (var p = 0.1; p <= 1.0; p += 0.1) { console.log(p + ", " + speedup(p, 2)); } |
for (var p = 0.1; p <= 1.0; p += 0.1) { console.log(p + ", " + speedup(p, 2)); }
And we can obtain the rough overall speed-up gains:
1.05, 1.11, 1.17, 1.25, 1.33, 1.42, 1.53, 1.66, 1.81 and 2.
Visualization of Ahmdal’s Law
The following shows the graphical representation of the speed-ups versus. the number of processors (N times faster), if the overhead (data transfer) is not considered.
And the following helps understand the fact that, we need to spend most resources in speed up the most performance critical parts, in order to achieve a greater overall performance gain.
–EOF (Coding For Speed) —
loading...
Next Post: Which is Faster? VBScript or JScript on WSH - A Performance Comparison using Sieve of Eratosthenes