Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
codewars test alex
(version: 0)
Comparing performance of:
my solution vs best solution
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function bestListPosition(word) { var indexer = {}; // D:3 B:1 A:0 C:2 var counts = []; // 2 1 1 1 var lettersCount = 0; word.split("").sort().forEach(function(x){ if ( indexer[x] == undefined ) { indexer[x] = lettersCount; counts[lettersCount] = 0; lettersCount ++; } }); var term = 1; var sum = term; word.split("").reverse().forEach(function(x, i){ var step = i + 1, idx = indexer[x]; counts[idx] ++; term /= counts[idx]; for (var j = 0; j < idx; ++j) if (counts[j] != 0) sum += term * counts[j]; term *= step; }); return sum; } function myListPosition(word) { console.log(word); var array = word.split(''); var sorted = array.slice().sort(); return word_helper(array, sorted); } function word_helper(array, sorted){ if (array.length === 0) return 1; var first = array[0]; if (first === sorted[0]){ return word_helper(array.slice(1), sorted.slice(1)); } var done=[]; var num = 0; for (let i = 0; sorted[i] !== first; i++){ if (done.length && done[done.length - 1] === sorted[i]) continue; done.push(sorted[i]); num += permutations(remove(array, sorted[i]), remove(sorted, sorted[i])); } return num + word_helper(array.slice(1), remove(sorted, first)); } function remove(array, element){ return array.slice(0, array.indexOf(element)) .concat(array.slice(array.indexOf(element) + 1)); } function permutations(array, sorted){ var dups = []; current = 0; for (let i = 1; i < sorted.length; i++){ if (sorted[i] === sorted[i-1]){ current += current ? 1 : 2; } else if (current){ dups.push(current); current = 0; } } if (current) dups.push(current); return factorial(array.length) / dups.reduce((p,c) => p * factorial(c), 1); } function factorial(n){ var ret = 1; for (; n > 0; n--){ ret *= n; } return ret; } test_words='thing|qergqergqebqergqerghwervqeg|wergqeruihoiuehfoiuh|gelrjflibqhf|greigubwreiugbqerg|qqergwrgqergfwrrth|bwnfofbbwoobgbjhwfhbbbhwofbbwofbgbw'.split('');
Tests:
my solution
for(word of test_words){ myListPosition(word); }
best solution
for (word of test_words){ bestListPosition(word); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
my solution
best solution
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 its components. **Benchmark Definition** The benchmark is testing two different implementations of a function called `myListPosition`. The function takes a word as input and returns a calculated value based on the position of certain letters in the alphabet. There are two versions of this function: "my solution" and "best solution". **Options being compared** The two options being compared are: 1. **myListPosition (my solution)**: This implementation uses a simple approach to calculate the value by splitting the word into an array, sorting it, and then calling a helper function `word_helper`. The helper function recursively calculates the value based on the position of letters in the alphabet. 2. **bestListPosition**: This implementation is identical to "my solution" but with a different name. **Pros and Cons** Both implementations have their pros and cons: * **myListPosition (my solution)** + Pros: - Easy to understand and implement - Uses built-in JavaScript functions (e.g., `split`, `sort`) + Cons: - May be slower due to the recursive nature of `word_helper` - May have higher memory usage due to the creation of multiple arrays * **bestListPosition** + Pros: - Optimized for performance, potentially faster than "my solution" - Uses less memory, as it avoids creating unnecessary arrays + Cons: - More complex implementation, harder to understand and implement - May use more advanced JavaScript features (e.g., `forEach`, `reduce`) **Library used** There is no explicit library mentioned in the benchmark definition. However, the implementation uses built-in JavaScript functions like `split`, `sort`, `forEach`, and `reduce`. These are part of the standard JavaScript library. **Special JS feature or syntax** The benchmark uses recursive function calls (e.g., `word_helper`) and a custom `permutations` function, which is not a standard JavaScript function. The `permutations` function calculates the number of permutations of an array with duplicate elements. **Alternatives** If you want to compare these implementations further, here are some alternatives: 1. **Use a different programming language**: Compare the performance of the same implementation in languages like Python, Java, or C++. 2. **Optimize one of the implementations**: Use tools like profilers or benchmarking libraries (e.g., WebPageTest) to optimize either "my solution" or `bestListPosition`. 3. **Compare with other algorithms**: Replace the `permutations` function with a different algorithm for calculating permutations, such as using mathematical formulas. Keep in mind that this is just one example of how you can explore alternatives. The specific approach depends on your goals and requirements.
Related benchmarks:
Immutable reverse
split vs loop
Slice + reverse vs manual picking array indexes
for loop reverce
MMP Reverse Array
Comments
Confirm delete:
Do you really want to delete benchmark?