Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark/ips'
- class A
- def sum
- 1 + 1
- end
- end
- class B
- def sum
- eval("1 + 1")
- end
- end
- class C
- define_method(:sum) {
- 1 + 1
- }
- end
- class D
- def sum
- Proc.new { 1 + 1 }.call
- end
- end
- class E
- def sum
- -> { 1 + 1 }.call
- end
- end
- Benchmark.ips do |x|
- x.config(:time => 20, :warmup => 2)
- x.report("Using method") do
- A.new.sum
- end
- x.report("Using eval") do
- B.new.sum
- end
- x.report("Using define_method") do
- C.new.sum
- end
- x.report("Using proc") do
- D.new.sum
- end
- x.report("Using lambda") do
- E.new.sum
- end
- x.compare!
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement