Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find prime gaps - with or without intermediate var
(version: 0)
Comparing performance of:
Use intermediate var vs Use repeated calculation
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fromNum = 2, toNum = 1000000; function isPrime(n) { for (var i = 2; i*i <= n; i++) { if ((n/i)%1 == 0) { return false; } } return true; } function primeGaps(start, end) { if (start < 1) {throw ''} result = {}; var currentPrime = null; var primeGap = null; for (var i = start; i <= end; i++) { if (isPrime(i)) { if (currentPrime !== null) { primeGap = i - currentPrime; if (!result.hasOwnProperty(primeGap)) { result[primeGap] = currentPrime; } } currentPrime = i; } } return result; } function primeGapsAlt(start, end) { if (start < 1) {throw ''} result = {}; var currentPrime = null; for (var i = start; i <= end; i++) { if (isPrime(i)) { if (currentPrime !== null) { if (!result.hasOwnProperty(i - currentPrime)) { result[i - currentPrime] = currentPrime; } } currentPrime = i; } } return result; }
Tests:
Use intermediate var
primeGaps(fromNum, toNum);
Use repeated calculation
primeGapsAlt(fromNum, toNum);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Use intermediate var
Use repeated calculation
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Use intermediate var
2.5 Ops/sec
Use repeated calculation
2.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is represented by two functions: `primeGaps` and `primeGapsAlt`. Both functions are designed to find prime gaps in a given range, which is defined by `fromNum` and `toNum`. **Function `primeGaps`** This function initializes an object `result` to store the prime gaps. It then iterates over the range from `start` to `end`, checking each number for primality using the `isPrime` function. If a number is prime, it calculates the difference between the current prime and the previous prime (`primeGap`) and stores it in the `result` object. **Function `primeGapsAlt`** This function is similar to `primeGaps`, but with a key difference: instead of storing the prime gap in the `result` object using `i - currentPrime` as the key, it uses `i - currentPrime` directly as the value. This approach avoids creating an intermediate variable for each prime gap. **Comparison** The two functions are compared to determine which one is faster. The `primeGaps` function creates an additional intermediate variable (`primeGap`) and uses a conditional statement to update it, while the `primeGapsAlt` function avoids these overheads by storing the value directly in the `result` object. **Pros and Cons** * **`primeGaps`**: + Pros: easier to understand and maintain, as it uses a clear variable name for the prime gap. + Cons: creates an additional intermediate variable, which can lead to more memory allocation and deallocation overhead. * **`primeGapsAlt`**: + Pros: avoids creating an intermediate variable, reducing memory allocation and deallocation overhead. + Cons: may be harder to understand and maintain, as the value is stored directly in the `result` object. **Library** There is no explicit library mentioned in the benchmark definition. However, the `isPrime` function is used to check for primality, which suggests that a custom implementation or a simple algorithm is being used. **Special JS feature/Syntax** None are explicitly mentioned in this benchmark.
Related benchmarks:
On2 vs Onlogn lengthOfLIS
Test for and for reverse
js custom yield vs base ma
JS BigInt big number performance v12
Number between/ Number in range
Comments
Confirm delete:
Do you really want to delete benchmark?