Benchmarking String Matching in Ruby
I am very prone to writing string matching as ‘string’.match(/ing/), which is very readable, however it is not always necessary to capture the matching part of the string. Having seen alternative syntax from a colleague I’ve used the following benchmark to test what is the most efficient syntax/method.
Robs-iMac:benchmarks$ irb
2.2.2 :001 > require 'benchmark'
=> true
2.2.2 :002 >
2.2.2 :003 > "cloudfront.net" =~ /cloudfront/
=> 0
2.2.2 :004 >
2.2.2 :005 > Benchmark.measure{ 1000000.times { "cloudfront.net" =~ /cloudfront/ } }
=> #<Benchmark::Tms:0x007fdea2288ee8 @label="", @real=0.30020256410352886, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.30000000000000004, @total=0.30000000000000004>
2.2.2 :006 >
2.2.2 :007 > "cloudfront.net"[/cloudfront/]
=> "cloudfront"
2.2.2 :008 >
2.2.2 :009 > Benchmark.measure{ 1000000.times { "cloudfront.net"[/cloudfront/] } }
=> #<Benchmark::Tms:0x007fdea24785a0 @label="", @real=0.36411550105549395, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.36, @total=0.36>
2.2.2 :010 >
2.2.2 :011 > "cloudfront.net".match(/cloudfront/)
=> #<MatchData "cloudfront">
2.2.2 :012 > Benchmark.measure{ 1000000.times { "cloudfront.net".match(/cloudfront/) } }
=> #<Benchmark::Tms:0x007fdea2350920 @label="", @real=0.967038004193455, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.9600000000000001, @total=0.9600000000000001>
I would appear that =~ is the outright winner, being at least three times faster. Keeping an eye out for unnecessary overuse of .match from now on. Referencing Stack Overflow http://stackoverflow.com/questions/11887145/fastest-way-to-check-if-a-string-matches-or-not-a-regexp-in-ruby