Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Large number product of x consecutive digits
(version: 0)
Comparing performance of:
Intuitive vs Fast vs Array
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * max); } function getLargeNumber(nl) { let number = getRandomInt(9999999999999999) + ''; // we'll keep the digits as a string, and make it into a string so we can just keep adding numbers to it to make it longer while(number.length <= nl) { number += getRandomInt(9999999999999999); } return number.substring(0, nl); }
Tests:
Intuitive
const nl = 50000; const number = getLargeNumber(nl); const gpl = 10; let greatestProduct = 0 let greatestProductPosition = 0; for(i = 0; i < nl-gpl; i++) { currentProduct = 1; for(j = i; j < i+gpl; j++) { currentProduct *= parseInt(number[j]); } if(currentProduct > greatestProduct) { greatestProduct = currentProduct; greatestProductPosition = i; } }
Fast
const nl = 50000; const number = getLargeNumber(nl); const gpl = 10; let greatestProduct = 0 let greatestProductPosition = 0; for(i = 0; i < nl; i++) { if(i >= gpl) { if(parseInt(number[i-gpl]) > 0) { if(currentProduct > 0) { currentProduct /= parseInt(number[i-gpl]); } } else { currentProduct = 1; for(j = i-(gpl-1); j < i; j++) { if(parseInt[j] > 0) { currentProduct *= parseInt(number[j]); } else { currentProduct = 0; break; } } } } currentProduct *= parseInt(number[i]); if(i >= (gpl-1) && currentProduct > greatestProduct) { greatestProduct = currentProduct; greatestProductPosition = i - (gpl-1); } }
Array
const nl = 50000; const number = getLargeNumber(nl); let split = []; const gpl = 10; let greatestProduct = 0 let greatestProductPosition = 0; let lastPos = 0; while((newPos = number.indexOf('0', lastPos)) !== -1) { if(newPos - lastPos > 0) { split.push([lastPos, newPos - lastPos]); } lastPos = newPos + 1; } if(nl - lastPos > 0) { split.push([lastPos, nl - lastPos]); } split.forEach(([start, length]) => { if(length < gpl) return; currentProduct = 1; for(i = start; i < (start + length); i++) { if(i >= (gpl + start)) { currentProduct /= parseInt(number[i-gpl]); } currentProduct *= parseInt(number[i]); if(i >= (gpl + start - 1) && currentProduct > greatestProduct) { greatestProduct = currentProduct; } } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Intuitive
Fast
Array
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 definition and individual test cases to explain what's being tested, compare options, and discuss pros and cons. **Benchmark Definition** The benchmark measures the performance of different approaches for finding the largest product of a contiguous substring in a large number. The number is generated by concatenating random digits. The goal is to find the starting position of the longest substring that can be multiplied together without exceeding a certain threshold (gpl). **Options Compared** Three test cases are provided: 1. **Intuitive**: This approach iterates through the entire string and keeps track of the current product. If the current character is greater than 0, it multiplies the product; otherwise, it resets the product to 1. 2. **Fast**: This approach uses a more complex algorithm that takes advantage of the structure of the input number. It first finds all occurrences of '0' and splits the string into substrings. Then, for each substring, it calculates the product while avoiding characters greater than 'gpl'. 3. **Array**: This is not explicitly described in the benchmark definition, but based on the provided code, it appears to be a simplified version of the Fast approach. **Pros and Cons** * **Intuitive**: + Pros: Easy to understand, straightforward implementation. + Cons: Inefficient, as it iterates through the entire string for each character. * **Fast**: + Pros: Optimized algorithm that takes advantage of the input structure, potentially faster than Intuitive. + Cons: More complex implementation, might be harder to understand for beginners. * **Array** (assuming this is a simplified version of Fast): + Pros: Similar optimization as Fast, but with fewer lines of code. + Cons: Might be less efficient due to the reduced complexity. **Library** The benchmark uses no external libraries. The `getRandomInt` function is defined in the script preparation code. **Special JS Features/Syntax** None mentioned explicitly, but the use of template literals (`''`) and arrow functions (`=>`) are examples of modern JavaScript features. **Other Alternatives** Alternative approaches might include: * Using a more efficient algorithm for finding contiguous substrings (e.g., rolling hash or suffix tree). * Optimizing the product calculation using arithmetic operations instead of string concatenation. * Utilizing parallel processing or multi-threading to speed up the calculation. Keep in mind that these alternatives would require significant changes to the benchmark definition and implementation.
Related benchmarks:
get precision from number string
getRandomNumberInRange vs getRandomValueInRange
getRandomNumberInRange vs getRandomValueInRange 5000
yoooooo
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
Comments
Confirm delete:
Do you really want to delete benchmark?