Created
August 8, 2023 21:43
-
-
Save technicalpickles/2c95c3938751413f3bd277aa1b9072cd to your computer and use it in GitHub Desktop.
Ruby performance: caller vs caller(1, n) vs stack_frame gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
require 'stack_frames' | |
STACK_FRAMES_BUFFER = StackFrames::Buffer.new(500) | |
def recurse(n, &block) | |
if n > 0 | |
recurse(n - 1, &block) | |
else | |
yield | |
end | |
end | |
recurse(1000) do | |
Benchmark.ips do |x| | |
x.config(time: 5, warmup: 1) | |
x.report("caller") { recurse(2000) { caller } } | |
x.report("caller(1, 500)") { recurse(2000) { caller(1, 500) }} | |
x.report("stack_frames") { recurse(2000) { STACK_FRAMES_BUFFER.capture } } | |
x.compare! | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Warming up -------------------------------------- | |
caller 151.000 i/100ms | |
caller(1, 500) 543.000 i/100ms | |
stack_frames 1.079k i/100ms | |
Calculating ------------------------------------- | |
caller 1.517k (± 2.8%) i/s - 7.701k in 5.082307s | |
caller(1, 500) 5.438k (± 0.8%) i/s - 27.693k in 5.092926s | |
stack_frames 10.851k (± 0.2%) i/s - 55.029k in 5.071541s | |
Comparison: | |
stack_frames: 10850.6 i/s | |
caller(1, 500): 5437.9 i/s - 2.00x slower | |
caller: 1516.7 i/s - 7.15x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment