Last active
March 5, 2019 05:56
-
-
Save pushrax/95946bd6a1567bdd280c5371ffd4e394 to your computer and use it in GitHub Desktop.
This file contains hidden or 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_driver' | |
require 'memory_profiler' | |
def assert_equal(a, b); raise "#{a} != #{b}" if a != b; end | |
report = MemoryProfiler.report { [:a, :b].include?(:a) } | |
assert_equal 1, report.total_allocated | |
Benchmark.driver do |x| | |
x.prelude(<<~RUBY) | |
require 'set' | |
CONST_ARRAY = [:a, :b, :c] | |
CONST_SET = Set.new(CONST_ARRAY) | |
v = :b | |
RUBY | |
x.report('literal Array#include?', r = <<~RUBY) | |
[:a, :b, :c].include?(v) | |
RUBY | |
x.report('const Array#include?', r = <<~RUBY) | |
CONST_ARRAY.include?(v) | |
RUBY | |
x.report('const Set#include?', r = <<~RUBY) | |
CONST_SET.include?(v) | |
RUBY | |
x.report('comparisons', r = <<~RUBY) | |
v == :a || v == :b || v == :c | |
RUBY | |
pp RubyVM::InstructionSequence.compile(r).to_a | |
end | |
Benchmark.driver do |x| | |
x.prelude(<<~RUBY) | |
require 'set' | |
CONST_ARRAY = ('a'..'f').map(&:to_sym) | |
CONST_SET = Set.new(CONST_ARRAY) | |
v = :e | |
RUBY | |
x.report('big const Array#include?', r = <<~RUBY) | |
CONST_ARRAY.include?(v) | |
RUBY | |
x.report('big const Set#include?', r = <<~RUBY) | |
CONST_SET.include?(v) | |
RUBY | |
end | |
""" | |
Warming up -------------------------------------- | |
literal Array#include? 13.221M i/s - 13.396M times in 1.013287s (75.64ns/i) | |
const Array#include? 21.427M i/s - 21.628M times in 1.009371s (46.67ns/i) | |
const Set#include? 18.530M i/s - 18.612M times in 1.004412s (53.97ns/i) | |
comparisons 30.259M i/s - 30.389M times in 1.004307s (33.05ns/i) | |
Calculating ------------------------------------- | |
literal Array#include? 18.574M i/s - 39.662M times in 2.135361s (53.84ns/i) | |
const Array#include? 33.423M i/s - 64.281M times in 1.923242s (29.92ns/i) | |
const Set#include? 28.247M i/s - 55.590M times in 1.967992s (35.40ns/i) | |
comparisons 59.466M i/s - 90.776M times in 1.526521s (16.82ns/i) | |
Comparison: | |
comparisons: 59466067.0 i/s | |
const Array#include?: 33423009.2 i/s - 1.78x slower | |
const Set#include?: 28246851.1 i/s - 2.11x slower | |
literal Array#include?: 18573982.1 i/s - 3.20x slower | |
Warming up -------------------------------------- | |
big const Array#include? 13.949M i/s - 14.000M times in 1.003688s (71.69ns/i) | |
big const Set#include? 18.059M i/s - 18.139M times in 1.004435s (55.38ns/i) | |
Calculating ------------------------------------- | |
big const Array#include? 19.402M i/s - 41.846M times in 2.156760s (51.54ns/i) | |
big const Set#include? 25.969M i/s - 54.176M times in 2.086178s (38.51ns/i) | |
Comparison: | |
big const Set#include?: 25968967.7 i/s | |
big const Array#include?: 19402342.4 i/s - 1.34x slower | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment