Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
two sum#2
(version: 0)
Comparing performance of:
brute force vs Memo vs hash vs innova
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
brute force
var twoSum = function(nums, target) { for(let i=0; i<nums.length; i++) { for(let j=i+1; j<nums.length; j++) { if(nums[i]+nums[j]===target) return [i,j] } } return []; }; twoSum([109,204,33,334,312,77,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,51,551,52,552,53,555,54,554,61,62,63,64,65,66,76,777,768,744,642,311,1,4,4,4,4,4,4,4,4,3,3,3,3,3,1,1,1,1,2,2,2,3,4,5,44,22,33,22,31,32,35,45,65,776,77,56,75,75,78,97,41,212,342,98,99,102,2,7,11,15], 9)
Memo
var twoSum = function(nums, target) { let sol = []; for(let i=0; i<nums.length; i++) { if(sol.length) break; for(let j=i+1; j<nums.length; j++) { if(sol.length) break; if(nums[i]+nums[j]===target) sol = [i,j] } } return sol; } twoSum([109,204,33,334,312,77,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,51,551,52,552,53,555,54,554,61,62,63,64,65,66,76,777,768,744,642,311,1,4,4,4,4,4,4,4,4,3,3,3,3,3,1,1,1,1,2,2,2,3,4,5,44,22,33,22,31,32,35,45,65,776,77,56,75,75,78,97,41,212,342,98,99,102,2,7,11,15], 9)
hash
var twoSum = function (nums, target) { let hashTable = [] let pair = [] let i = 0; let foundPair = false while (i < nums.length) { hashTable[nums[i]] = i i += 1 } i = 0 while (i < nums.length && !foundPair) { let compliment = target - nums [i] if (compliment in hashTable) { if (i !== hashTable[compliment]) { pair.push(i) pair.push(hashTable [compliment]) foundPair = true } } i += 1 } return pair } twoSum([109,204,33,334,312,77,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,51,551,52,552,53,555,54,554,61,62,63,64,65,66,76,777,768,744,642,311,1,4,4,4,4,4,4,4,4,3,3,3,3,3,1,1,1,1,2,2,2,3,4,5,44,22,33,22,31,32,35,45,65,776,77,56,75,75,78,97,41,212,342,98,99,102,2,7,11,15], 9)
innova
const twoSum = function(nums, target) { const comp = {}; for(let i=0; i<nums.length; i++){ if(comp[nums[i]] >=0 ){ return [comp[nums[i]] , i] } comp[target-nums[i]] = i } }; twoSum([109,204,33,334,312,77,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,51,551,52,552,53,555,54,554,61,62,63,64,65,66,76,777,768,744,642,311,1,4,4,4,4,4,4,4,4,3,3,3,3,3,1,1,1,1,2,2,2,3,4,5,44,22,33,22,31,32,35,45,65,776,77,56,75,75,78,97,41,212,342,98,99,102,2,7,11,15], 9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
brute force
Memo
hash
innova
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):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of different algorithms for finding two sum in an array of numbers. The input data is provided in the form of a JSON object, which contains multiple test cases with varying sizes and complexities. **Test Cases** There are four test cases: 1. **Hash**: This algorithm uses a hash table to store the elements as it iterates through the array. When it encounters an element that has already been seen before (i.e., its index is present in the hash table), it returns the current index and the previous index of the element. 2. **Innova**: This algorithm also uses a hash-like data structure, but with some differences in implementation details. It has a constant-time look-up and insertion operation. 3. **Brute Force**: This algorithm simply iterates through the array and checks every pair of elements to find a sum equal to the target value. This approach has a time complexity of O(n^2). 4. **Memo**: This algorithm uses memoization to store the results of subproblems (i.e., sums of pairs) as it iterates through the array. **Performance Metrics** The benchmark measures the number of executions per second (ExecutionsPerSecond) for each test case. This metric indicates how many times the algorithm can execute in a second, which is a common way to measure performance. **Observations and Insights** Based on the latest benchmark results: * The **Hash** algorithm has the highest execution rate (178838.5625 executions per second), indicating that it is likely to be the fastest algorithm for this problem. * The **Innova** algorithm has a lower execution rate than the **Hash** algorithm, but still outperforms the **Brute Force** algorithm (103407.6484375 executions per second). * The **Memo** algorithm has the lowest execution rate among all test cases, indicating that it may be less efficient or have higher overhead compared to other algorithms. Overall, these results suggest that the **Hash** algorithm is likely to be the winner for this benchmark, followed closely by the **Innova** algorithm.
Related benchmarks:
twoSum test
Compare Lodash and JS
builtin plus operator vs. custom sum method
two sum
Comments
Confirm delete:
Do you really want to delete benchmark?