Skip to content

Instantly share code, notes, and snippets.

@tonytonyjan
Last active September 20, 2017 14:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tonytonyjan/8560274d4c1c058ebf134c73078f4698 to your computer and use it in GitHub Desktop.
`[4, 5].max` is slower than `[a, b].max` with ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
require 'benchmark'
n = 100000000
Benchmark.bmbm do |x|
x.report('[4, 5].max') { n.times { [4, 5].max } }
a, b = 4, 5
x.report('[a, b].max') { n.times { [a, b].max } }
end
puts RubyVM::InstructionSequence.compile('[4, 5].max').disasm
# == disasm: #<ISeq:<compiled>@<compiled>>================================
# 0000 trace 1 ( 1)
# 0002 duparray [4, 5]
# 0004 opt_send_without_block <callinfo!mid:max, argc:0, ARGS_SIMPLE>, <callcache>
# 0007 leave
puts RubyVM::InstructionSequence.compile('x, y = 4, 5; [x, y].max').disasm
# == disasm: #<ISeq:<compiled>@<compiled>>================================
# local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
# [ 2] x [ 1] y
# 0000 trace 1 ( 1)
# 0002 putobject 4
# 0004 putobject 5
# 0006 setlocal_OP__WC__0 3
# 0008 setlocal_OP__WC__0 4
# 0010 getlocal_OP__WC__0 4
# 0012 getlocal_OP__WC__0 3
# 0014 opt_newarray_max 2
# 0016 leave
Rehearsal ----------------------------------------------
[4, 5].max 9.600000 0.030000 9.630000 ( 9.688531)
[a, b].max 4.720000 0.020000 4.740000 ( 4.794517)
------------------------------------ total: 14.370000sec
user system total real
[4, 5].max 9.730000 0.030000 9.760000 ( 9.792203)
[a, b].max 4.410000 0.000000 4.410000 ( 4.413312)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment