Permalink
Browse files

Perf: Set#include? is O(1); Array#include? is O(N).

See the included benchmarks for actual benchmarks against a collection of 7 values like we have here.
  • Loading branch information...
1 parent 0102504 commit 40f83a9d6b8820d57c2b2ecc20f193b1d1f55f71 @myronmarston myronmarston committed Jan 12, 2012
Showing with 71 additions and 1 deletion.
  1. +68 −0 benchmarks/set_vs_array_include.rb
  2. +3 −1 lib/rspec/matchers/matcher.rb
View
68 benchmarks/set_vs_array_include.rb
@@ -0,0 +1,68 @@
+require 'benchmark'
+require 'set'
+
+n = 10000000
+
+array = [
+ :@name, :@declarations, :@diffable, :@messages,
+ :@match_block, :@match_for_should_not_block,
+ :@expected_exception
+]
+set = array.to_set
@alindeman
alindeman Jan 17, 2012

It seems like converting the array to a set should count against the set benchmark.

@dchelimsky
dchelimsky Jan 17, 2012

The conversion happens once per runtime (it's a constant), whereas the include check happens n times per runtime.

@alindeman
alindeman Jan 17, 2012

Ah, gotcha, misread the production code.

+
+puts "Positive examples: "
+Benchmark.bm(25) do |x|
+ array.each_with_index do |var, i|
+ x.report("set.include?(item #{i}) ") do
+ n.times { set.include?(var) }
+ end
+
+ x.report("array.include?(item #{i})") do
+ n.times { array.include?(var) }
+ end
+
+ puts "=" * 80
+ end
+end
+
+puts "\n\nNegative examples: "
+Benchmark.bm(5) do |x|
+ x.report("set ") do
+ n.times { set.include?(:@other) }
+ end
+
+ x.report("array") do
+ n.times { array.include?(:@other) }
+ end
+end
+
+# Positive examples:
+# user system total real
+# set.include?(item 0) 2.000000 0.010000 2.010000 ( 1.999305)
+# array.include?(item 0) 1.170000 0.000000 1.170000 ( 1.173168)
+# ================================================================================
+# set.include?(item 1) 2.020000 0.000000 2.020000 ( 2.016389)
+# array.include?(item 1) 1.580000 0.000000 1.580000 ( 1.585301)
+# ================================================================================
+# set.include?(item 2) 1.980000 0.010000 1.990000 ( 1.984699)
+# array.include?(item 2) 2.170000 0.000000 2.170000 ( 2.167163)
+# ================================================================================
+# set.include?(item 3) 2.110000 0.010000 2.120000 ( 2.125914)
+# array.include?(item 3) 2.450000 0.000000 2.450000 ( 2.445224)
+# ================================================================================
+# set.include?(item 4) 2.090000 0.010000 2.100000 ( 2.094182)
+# array.include?(item 4) 2.920000 0.000000 2.920000 ( 2.924850)
+# ================================================================================
+# set.include?(item 5) 2.000000 0.000000 2.000000 ( 2.000656)
+# array.include?(item 5) 3.540000 0.010000 3.550000 ( 3.547563)
+# ================================================================================
+# set.include?(item 6) 2.030000 0.000000 2.030000 ( 2.032430)
+# array.include?(item 6) 3.800000 0.010000 3.810000 ( 3.810014)
+# ================================================================================
+
+
+# Negative examples:
+# user system total real
+# set 1.940000 0.000000 1.940000 ( 1.941780)
+# array 4.240000 0.010000 4.250000 ( 4.238137)
+
View
4 lib/rspec/matchers/matcher.rb
@@ -1,3 +1,5 @@
+require 'set'
+
module RSpec
module Matchers
module DSL
@@ -30,7 +32,7 @@ def initialize(name, &declarations)
:@name, :@declarations, :@diffable, :@messages,
:@match_block, :@match_for_should_not_block,
:@expected_exception
- ]
+ ].to_set
# @api private
def for_expected(*expected)

0 comments on commit 40f83a9

Please sign in to comment.