Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Powers of two
(version: 0)
If they should be written in an array or in the loop as we go
Comparing performance of:
for with if vs 2x for vs 2x for charcode-48 vs mm vs oo
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
for with if
var j = "MSKU947765"; var s = 0; var r = [1,2,4,8,16,32,64,128,256,512]; for (var e=0;e<10;e++) { if (e<4) { var c = j.charCodeAt(e)-55; s+=(Math.floor(c/10.2)+c)*r[e]; } else { s+=Number(j.charAt(e))*r[e]; }; }; var d = s % 11 % 10; return d;
2x for
var j = "MSKU947765"; var s = 0; var r = [1,2,4,8,16,32,64,128,256,512]; for (var e=0;e<4;e++) { var c = j.charCodeAt(e)-55; s+=(Math.floor(c/10.2)+c)*r[e]; }; for (var f=0;f<6;f++){ s+=Number(j.charAt(e))*r[e]; }; var d = s % 11 % 10; return d;
2x for charcode-48
var j = "MSKU947765"; var s = 0; var r = [1,2,4,8,16,32,64,128,256,512]; for (var e=0;e<4;e++) { var c = j.charCodeAt(e)-55; s+=(Math.floor(c/10.2)+c)*r[e]; }; for (var f=0;f<6;f++){ s+=(j.charCodeAt(e)-48)*r[e]; }; var d = s % 11 % 10; return d;
mm
var j = "MSKU947765"; var s = 0; var r = [1,2,4,8,16,32,64,128,256,512]; for (var e=0;e<4;e++) { var c = j.charCodeAt(e)-55; s+=(Math.floor(c/10.2)+c)*r[e]; }; for (var e=4;e<10;e++){ s+=(j.charCodeAt(e)-48)*r[e]; }; var d = s % 11 % 10; return d;
oo
var j = "MSKU947765"; var sum = 0; for (var i = 0; i < 10; i++) { var m = j.charCodeAt(i); if (i < 4) { m -= 55; m += parseInt(m / 11); } else { m -= 48; } sum += m * Math.pow(2, i); } var d = sum % 11; if (d === 10) d = 0; return d;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for with if
2x for
2x for charcode-48
mm
oo
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures the performance of different approaches to calculate a value represented as a hexadecimal string "MSKU947765". The goal is to find the fastest way to perform this calculation. **Options Compared** There are four options compared: 1. **"for with if"`**: This option uses a traditional `for` loop with an `if` statement inside it. 2. **"2x for"`**: This option uses two separate `for` loops, one for the first 4 iterations and another for the remaining 6 iterations. 3. **"2x for charcode-48"`**: This option is similar to "2x for", but it uses the character code 48 (instead of subtracting 55) for the second loop. 4. **"mm"`**: This option uses a different approach with a single `for` loop, but with some optimizations (more on this later). 5. **"oo"`**: This option is similar to "mm", but with a different optimization. **Pros and Cons of Each Approach** Here's a brief summary: * **"for with if"`**: Simple and straightforward, but might be slower due to the overhead of the `if` statement. + Pros: Easy to understand and maintain. + Cons: Might not be optimized for performance. * **"2x for"`**: Uses two loops, which can lead to more overhead. + Pros: Allows for separate optimization of each loop. + Cons: More complex code, harder to understand. * **"2x for charcode-48"`**: Similar to "2x for", but with a different character code. + Pros: Same optimizations as "2x for". + Cons: Might not be suitable if the character code changes. * **"mm"`: Uses a single `for` loop, but with some optimizations. + Pros: Optimized for performance and readability. + Cons: More complex logic, harder to understand. * **"oo"`: Similar to "mm", but with a different optimization. + Pros: Same optimizations as "mm". + Cons: Might not be suitable if the optimization changes. **Library Used** There is no library explicitly mentioned in the benchmark definition or test cases. However, some libraries might be used implicitly (e.g., `Math` for mathematical functions). **Optimizations** The `mm` and `oo` options use optimizations that are not immediately clear from the code. These optimizations likely involve: * Loop unrolling: Expanding the loop to reduce overhead. * Cache optimization: Optimizing memory access patterns. * Branch prediction: Optimizing branch instructions. These optimizations can improve performance, but might also make the code harder to understand. **Browser and Device Information** The benchmark results are reported for Firefox 53 on Windows 7. The device platform is "Other", which suggests that the test was run on a virtual machine or an emulator. Overall, this benchmark highlights the importance of optimization techniques in improving performance, but also shows that even with optimized code, different browsers and devices can produce varying results.
Related benchmarks:
Powers of two
Powers of two
pow vs multiply v2
Math.pow(2,n) vs Table lookup vs bitwise
Math.pow vs multiplication with decimals and multiple integers
Comments
Confirm delete:
Do you really want to delete benchmark?