Skip to content

Instantly share code, notes, and snippets.

@motchang
Last active August 12, 2021 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motchang/6a49972b817aa2818b3a686a02ca8e3d to your computer and use it in GitHub Desktop.
Save motchang/6a49972b817aa2818b3a686a02ca8e3d to your computer and use it in GitHub Desktop.
String#include? ってそんなに遅いものかね? https://techracho.bpsinc.jp/hachi8833/2019_10_18/82048
$ ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin19]
# frozen_string_literal: true
require 'benchmark/ips'
SLUG = 'test_some_kind_of_long_file_name.rb'
def slower
SLUG =~ /^test_/
end
def slow
SLUG.match?(/^test_/)
end
def fast_start
SLUG.start_with?('test_')
end
def fast_end
SLUG.end_with?('rb')
end
def fast_include
SLUG.include?('_long_')
end
Benchmark.ips do |x|
x.report('String#=~') { slower }
x.report('String#match?') { slow } if RUBY_VERSION >= '2.4.0'
x.report('String#start_with?') { fast_start }
x.report('String#end_with?') { fast_end }
x.report('String#include?') { fast_include }
x.compare!
end
Warming up --------------------------------------
String#=~ 132.956k i/100ms
String#match? 782.020k i/100ms
String#start_with? 1.268M i/100ms
String#end_with? 1.243M i/100ms
String#include? 891.121k i/100ms
Calculating -------------------------------------
String#=~ 1.288M (± 5.3%) i/s - 6.515M in 5.073370s
String#match? 7.835M (± 1.0%) i/s - 39.883M in 5.090763s
String#start_with? 12.618M (± 1.0%) i/s - 63.403M in 5.025501s
String#end_with? 12.427M (± 0.4%) i/s - 62.136M in 5.000110s
String#include? 8.865M (± 0.4%) i/s - 44.556M in 5.026249s
Comparison:
String#start_with?: 12617714.5 i/s
String#end_with?: 12427123.9 i/s - 1.02x (± 0.00) slower
String#include?: 8864800.7 i/s - 1.42x (± 0.00) slower
String#match?: 7835167.0 i/s - 1.61x (± 0.00) slower
String#=~: 1288148.5 i/s - 9.80x (± 0.00) slower
# frozen_string_literal: true
require 'benchmark/ips'
SLUG = 'test_some_kind_of_long_file_name.rb'
def slower
SLUG =~ /^test_/
end
def slow
SLUG.match?(/^test_/)
end
def fast_start
SLUG.start_with?('test_')
end
def fast_end
SLUG.end_with?('rb')
end
def fast_include
SLUG.include?('test_')
end
Benchmark.ips do |x|
x.report('String#=~') { slower }
x.report('String#match?') { slow } if RUBY_VERSION >= '2.4.0'
x.report('String#start_with?') { fast_start }
x.report('String#end_with?') { fast_end }
x.report('String#include?') { fast_include }
x.compare!
end
Warming up --------------------------------------
String#=~ 131.442k i/100ms
String#match? 776.953k i/100ms
String#start_with? 1.247M i/100ms
String#end_with? 1.278M i/100ms
String#include? 1.039M i/100ms
Calculating -------------------------------------
String#=~ 1.325M (± 1.1%) i/s - 6.704M in 5.059214s
String#match? 7.836M (± 1.0%) i/s - 39.625M in 5.056976s
String#start_with? 12.467M (± 0.5%) i/s - 62.370M in 5.003090s
String#end_with? 12.734M (± 0.4%) i/s - 63.882M in 5.016861s
String#include? 10.335M (± 0.4%) i/s - 51.972M in 5.028914s
Comparison:
String#end_with?: 12733562.7 i/s
String#start_with?: 12466708.5 i/s - 1.02x (± 0.00) slower
String#include?: 10334859.3 i/s - 1.23x (± 0.00) slower
String#match?: 7836438.7 i/s - 1.62x (± 0.00) slower
String#=~: 1325177.7 i/s - 9.61x (± 0.00) slower
$ ruby -v
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]
Warming up --------------------------------------
String#=~ 127.098k i/100ms
String#match? 710.754k i/100ms
String#start_with? 1.092M i/100ms
String#end_with? 1.048M i/100ms
String#include? 771.099k i/100ms
Calculating -------------------------------------
String#=~ 1.273M (± 1.7%) i/s - 6.482M in 5.091987s
String#match? 7.102M (± 6.4%) i/s - 35.538M in 5.029916s
String#start_with? 10.944M (± 1.8%) i/s - 55.693M in 5.090621s
String#end_with? 10.445M (± 2.5%) i/s - 52.418M in 5.022161s
String#include? 7.654M (± 3.8%) i/s - 38.555M in 5.046023s
Comparison:
String#start_with?: 10944206.6 i/s
String#end_with?: 10445451.4 i/s - 1.05x (± 0.00) slower
String#include?: 7653534.3 i/s - 1.43x (± 0.00) slower
String#match?: 7101813.0 i/s - 1.54x (± 0.00) slower
String#=~: 1273404.5 i/s - 8.59x (± 0.00) slower
Warming up --------------------------------------
String#=~ 128.314k i/100ms
String#match? 678.231k i/100ms
String#start_with? 1.083M i/100ms
String#end_with? 1.080M i/100ms
String#include? 929.259k i/100ms
Calculating -------------------------------------
String#=~ 1.276M (± 2.9%) i/s - 6.416M in 5.034819s
String#match? 7.267M (± 3.2%) i/s - 36.624M in 5.045871s
String#start_with? 10.802M (± 0.6%) i/s - 54.156M in 5.013875s
String#end_with? 10.799M (± 0.4%) i/s - 55.063M in 5.099252s
String#include? 9.254M (± 1.9%) i/s - 46.463M in 5.022914s
Comparison:
String#start_with?: 10801652.3 i/s
String#end_with?: 10798549.8 i/s - same-ish: difference falls within error
String#include?: 9253799.1 i/s - 1.17x (± 0.00) slower
String#match?: 7266531.5 i/s - 1.49x (± 0.00) slower
String#=~: 1275569.6 i/s - 8.47x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment