Last active
January 27, 2021 18:37
-
-
Save britishtea/69f4e1a4524ef2dfefc3 to your computer and use it in GitHub Desktop.
Array#join vs Regexp.union (ran on Ruby 2.0.0)
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/ips" | |
input = %w[one two three four five six seven] | |
Benchmark.ips do |x| | |
x.report "Array#join" do | |
Regexp.new input.map { |e| Regexp.escape e }.join "|" | |
end | |
x.report "Regexp.union" do | |
Regexp.union input | |
end | |
x.compare! | |
end |
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
$ ruby -v | |
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin13] | |
$ ruby benchmark.rb | |
Calculating ------------------------------------- | |
Array#join 4044 i/100ms | |
Regexp.union 3947 i/100ms | |
------------------------------------------------- | |
Array#join 46052.1 (±3.6%) i/s - 230508 in 5.012008s | |
Regexp.union 43411.7 (±4.0%) i/s - 217085 in 5.009206s | |
Comparison: | |
Array#join: 46052.1 i/s | |
Regexp.union: 43411.7 i/s - 1.06x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment