Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multiplication vs Additions
(version: 0)
Comparing performance of:
Multiplication vs Additions
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const randInt = () => Math.floor(Math.random() * 100); var arr = Array.from({ length: 10000 }, () => randInt());
Tests:
Multiplication
let total = 0; for (const v of arr) { total += 100 * v; } console.log(total);
Additions
let total = 0; for (const v of arr) { for (let i = 100; 0 < i--;) total += v; } console.log(total);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Multiplication
Additions
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 146 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Multiplication
19327.6 Ops/sec
Additions
457.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark created on the MeasureThat.net website. The benchmark tests two different approaches for calculating the sum of an array of numbers: multiplication and additions. **Test Cases** There are two test cases: 1. **Multiplication**: This test case uses a single loop to multiply each number in the array by 100 and then adds up the results. 2. **Additions**: This test case uses a nested loop structure to add each number in the array to itself 100 times. **Libraries** There is no explicit library mentioned in the benchmark definition or test cases. However, the use of `Array.from()` suggests that the JavaScript engine being tested supports this method for creating arrays from iterables. **Special JS Features/Syntax** * None are explicitly used in the benchmark code. **Options Compared** The two approaches compared in the benchmark are: 1. **Multiplication**: This approach uses a single loop to multiply each number in the array by 100 and then adds up the results. 2. **Additions**: This approach uses a nested loop structure to add each number in the array to itself 100 times. **Pros and Cons of Each Approach** * **Multiplication**: + Pros: - Likely to be faster than the additions approach, since it involves fewer iterations. - May have better cache locality due to the multiplication operation being a single, large calculation. + Cons: - May have higher overhead due to the need to multiply each number by 100. * **Additions**: + Pros: - May have lower overhead compared to multiplication, since it involves fewer operations. + Cons: - Likely to be slower than multiplication, since it involves more iterations. - May suffer from poor cache locality due to the nested loop structure. **Other Considerations** * **Branch Prediction**: The additions approach may suffer from branch prediction errors, as the inner loop condition (`0 < i--`) is not always predictable. * **Cache Locality**: The multiplication approach may have better cache locality due to the large, single calculation involved in multiplying each number by 100. **Alternatives** Other approaches that could be used to calculate the sum of an array of numbers include: 1. **Reducing Array**: Using `Array.prototype.reduce()` or a similar method to add up the numbers in the array. 2. **For...of Loop with Spread Operator**: Using a `for...of` loop and the spread operator (`...`) to iterate over the array and add up its elements. These alternative approaches may have different performance characteristics compared to the multiplication and additions approaches used in the benchmark.
Related benchmarks:
array vs float32array without conversion
Lodash 4.17.21 sort vs array.prototype.sort
array vs float64 for io and slice
array vs float64 for io and slice (fixed)
Lodash.isArray vs Array.isArray (Lodash v4.17.15)
Comments
Confirm delete:
Do you really want to delete benchmark?