Skip to content

Instantly share code, notes, and snippets.

@pushrax
Last active March 5, 2019 05:31
Show Gist options
  • Save pushrax/2ee20a276151279257edb49246d30d96 to your computer and use it in GitHub Desktop.
Save pushrax/2ee20a276151279257edb49246d30d96 to your computer and use it in GitHub Desktop.
require 'benchmark_driver'
require 'memory_profiler'
def assert_equal(a, b); raise "#{a} != #{b}" if a != b; end
# Great!
a = 1
report = MemoryProfiler.report { [a, 2].max }
assert_equal 0, report.total_allocated
# Can't be only literals
report = MemoryProfiler.report { [1, 2].max }
assert_equal 1, report.total_allocated
# Can't assign to a local
report = MemoryProfiler.report { arr = [a, 2]; arr.max }
assert_equal 1, report.total_allocated
# Can't have splats
a = [1, 2]
report = MemoryProfiler.report { [*a].max }
assert_equal 1, report.total_allocated
# 256 size
a = 1
report = MemoryProfiler.report { [a,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1].max }
assert_equal 0, report.total_allocated
# 257 size
report = MemoryProfiler.report { [a,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1].max }
assert_equal 3, report.total_allocated
Benchmark.driver do |x|
x.prelude(<<~RUBY)
a, b, c = 1, 2, 3
RUBY
x.report('array literal 3', r = <<~RUBY)
[a, b, c].max
RUBY
x.report('ternaries 3', r = <<~RUBY)
a > b ? (a > c ? a : c) : (b > c) ? b : c
RUBY
x.report('array literal 2', r = <<~RUBY)
[a, b].max
RUBY
pp RubyVM::InstructionSequence.compile(r).to_a
x.report('ternaries 2', r = <<~RUBY)
a > b ? a : b
RUBY
pp RubyVM::InstructionSequence.compile(r).to_a
end
"""
Warming up --------------------------------------
array literal 3 29.656M i/s - 29.860M times in 1.006881s (33.72ns/i)
ternaries 3 32.848M i/s - 32.854M times in 1.000189s (30.44ns/i)
array literal 2 32.670M i/s - 32.714M times in 1.001368s (30.61ns/i)
ternaries 2 42.152M i/s - 42.208M times in 1.001335s (23.72ns/i)
Calculating -------------------------------------
array literal 3 55.860M i/s - 88.968M times in 1.592686s (17.90ns/i)
ternaries 3 67.531M i/s - 98.543M times in 1.459232s (14.81ns/i)
array literal 2 74.307M i/s - 98.009M times in 1.318977s (13.46ns/i)
ternaries 2 138.827M i/s - 126.456M times in 0.910889s (7.20ns/i)
Comparison:
ternaries 2: 138827175.4 i/s
array literal 2: 74306596.7 i/s - 1.87x slower
ternaries 3: 67531067.7 i/s - 2.06x slower
array literal 3: 55860046.5 i/s - 2.49x slower
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment