-
-
Save leonid-shevtsov/d01ef055133bcab6b24a7a496ff6cb5b to your computer and use it in GitHub Desktop.
This file contains 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" | |
require "strscan" | |
string = "abc" * 10_000 | |
Benchmark.ips do |x| | |
x.report("scan") do | |
accum = "" | |
string.scan(/\w/) { |m| accum = m } | |
end | |
x.report("stringscanner") do | |
accum = "" | |
scanner = StringScanner.new(string) | |
loop do | |
break if scanner.eos? | |
accum = scanner.scan(/\w/) | |
end | |
end | |
x.compare! | |
end | |
# ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22] | |
# | |
# Warming up -------------------------------------- | |
# scan 12.000 i/100ms | |
# stringscanner 32.000 i/100ms | |
# Calculating ------------------------------------- | |
# scan 119.745 (± 2.5%) i/s - 600.000 in 5.014112s | |
# stringscanner 328.416 (± 1.5%) i/s - 1.664k in 5.067770s | |
# Comparison: | |
# stringscanner: 328.4 i/s | |
# scan: 119.7 i/s - 2.74x (± 0.00) slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment