Skip to content

Instantly share code, notes, and snippets.

@postmodern
Last active March 9, 2022 01:33
Show Gist options
  • Save postmodern/4602464080e30952810b191da8a6128e to your computer and use it in GitHub Desktop.
Save postmodern/4602464080e30952810b191da8a6128e to your computer and use it in GitHub Desktop.
A micro-benchmark of Ruby's `String#include? || String#include?` vs. `String#=~ /[...]/`
#!/usr/bin/env ruby
require 'benchmark'
Benchmark.bm(20) do |b|
n = 10_000
string = ('A' * n) + 'x'
b.report('String#include?(...))') do
n.times do
string.include?(',') || string.include?('x')
end
end
b.report('String=~ /[...]/') do
n.times do
string =~ /[,x]/
end
end
end
user system total real
String#include?(...)) 0.003851 0.000960 0.004811 ( 0.004902)
String=~ /[...]/ 0.087985 0.000000 0.087985 ( 0.088354)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment