Last active
March 9, 2022 01:33
-
-
Save postmodern/4602464080e30952810b191da8a6128e to your computer and use it in GitHub Desktop.
A micro-benchmark of Ruby's `String#include? || String#include?` vs. `String#=~ /[...]/`
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
#!/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 |
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
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