Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Stack strategies
(version: 0)
Comparing performance of:
generators vs Manual
Created:
9 years ago
by:
Guest
Jump to the latest result
Tests:
generators
var GAS = 0; function run(init, makeGen) { GAS = init; var gen = makeGen(); while(true) { var res = gen.next(); if(res.done) { return res.value; } else { GAS = init; continue; } } } var linkNames = ["first", "rest"]; function* link(f, r) { return { $name: "link", $fields: linkNames, first: f, rest: r } } var empty = { $name: "empty" }; function* map(f, l) { if(GAS-- <= 0) { yield null; } var ans; var dispatch = { empty: 0, link: 1 }; var case_step = dispatch[l.$name]; switch(case_step) { case 0: ans = empty break; case 1: var fst = l[l.$fields[0]]; var rst = l[l.$fields[1]]; var t1 = yield* f(fst); var t2 = yield* map(f, rst); var t3 = yield* link(t1, t2); ans = t3; } return ans; } var buildList = function(n) { var l = empty; for(var i = 0; i < n; i++) { l = link(i, l).next().value; } return l; } run(100, function*() { return yield* map(function*(l) { return l + 1; }, buildList(4000)); });
Manual
var GAS = 0; function run(init, runner) { GAS = init; var frames = [{ run: runner }]; var thisFrame = frames.pop(); while(true) { var res = thisFrame.run(thisFrame); if(res.isCont) { var len = res.frames.length; for(var i = 0; i < len; i++) { frames.push(res.frames.pop()); } GAS = init; thisFrame = frames.pop(); } else { if(frames.length <= 0) { break; } thisFrame = frames.pop(); thisFrame.ans = res; } } return res; } var linkNames = ["first", "rest"]; function link(f, r) { return { $name: "link", $fields: linkNames, first: f, rest: r } } var empty = { $name: "empty" }; function map(f, l) { var step = 0; if(f.isFrame) { var ans = f.ans; step = f.step; var t1 = f.vars[0]; var fst = f.vars[1]; var rst = f.vars[2]; var t2 = f.vars[3]; l = f.args[1]; f = f.args[0]; } if(GAS-- <= 0) { return { isCont: true, frames: [{ run: map, isFrame: true, vars: [t1, fst, rst, t2], args: [f, l], step: step }] }; } while(true) { switch(step) { case 0: var dispatch = { empty: 1, link: 2 }; step = dispatch[l.$name]; break; case 1: ans = empty step = 5 break; case 2: fst = l[l.$fields[0]]; rst = l[l.$fields[1]]; step = 3; ans = f(fst); break; case 3: t1 = ans; step = 4 ans = map(f, rst); break; case 4: t2 = ans; step = 5 ans = link(t1, t2); break; case 5: GAS++; return ans; } if(ans && ans.isCont) { ans.frames.push({ run: map, isFrame: true, vars: [t1, fst, rst, t2], args: [f, l], step: step }); return ans; } } } var buildList = function(n) { var l = empty; for(var i = 0; i < n; i++) { l = link(i, l); } return l; } run(100, function() { return map(function(l) { return l + 1; }, buildList(4000)); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
generators
Manual
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 provide an in-depth explanation of the benchmark, its options, pros and cons, and other considerations. **Benchmark Overview** The provided JSON represents two test cases for measuring performance differences between three stack strategies: Manual, Generators, and GAS (Google's Iterative Template System). **Test Case 1: "generators"** This test case uses the `Generators` strategy. In this approach, a generator function (`map`) is used to traverse a linked list (`buildList`). The generator yields values one by one, allowing for efficient iteration. Here are the key components: * `buildList`: Creates a linked list with `n` elements. * `map`: A recursive generator function that traverses the linked list and returns incremented values. **Test Case 2: "Manual"** This test case uses the Manual strategy. In this approach, the program explicitly iterates over the linked list using a loop. Here are the key components: * `buildList`: Creates a linked list with `n` elements. * A simple loop that traverses the linked list and returns incremented values. **Test Case 3: "GAS"** This test case uses GAS, but its implementation is not provided in the JSON. We can infer its general structure based on the context: * `buildList`: Creates a linked list with `n` elements. * A recursive function that traverses the linked list using a call stack. **Options and Performance Characteristics** The three strategies have different performance characteristics: 1. **Manual**: Iterates over the linked list explicitly, which can lead to slower performance due to overhead from explicit loop control. 2. **Generators**: Uses a generator function to traverse the linked list, allowing for efficient iteration with minimal overhead. 3. **GAS**: Uses an iterative template system to traverse the linked list, potentially offering better performance than manual iteration. **Pros and Cons** Here are some pros and cons for each strategy: 1. **Manual**: * Pros: Easy to understand, implement, and debug. * Cons: Can be slower due to explicit loop control. 2. **Generators**: * Pros: Efficient iteration with minimal overhead, easy to understand. * Cons: May require additional setup or boilerplate code for generator functions. 3. **GAS**: * Pros: Potential for better performance, but implementation details are not provided in the JSON. * Cons: Unknown performance characteristics without more information. **Other Considerations** The benchmark results show that the Manual strategy is significantly faster than both Generators and GAS (although GAS's implementation is not provided). This may be due to the overhead of setting up generator functions or the iterative template system used in GAS. In conclusion, the choice of stack strategy depends on performance requirements, code complexity, and maintainability. While Generators offer efficient iteration with minimal overhead, Manual iteration might be suitable for simple cases where speed is not a critical concern. The performance characteristics of GAS are unknown without more information, but it may potentially offer better performance in certain scenarios. I hope this detailed explanation helps you understand the benchmark and its options!
Related benchmarks:
Reduce to array
Stack vs Queue
spread vs push set creation 2
fill vs push multiple
fill vs push multiple small
Comments
Confirm delete:
Do you really want to delete benchmark?