Skip to content

Instantly share code, notes, and snippets.

@miharekar
Last active September 22, 2020 06:20
Show Gist options
  • Save miharekar/c99c57883ba44a00dd36bea71caf3f9d to your computer and use it in GitHub Desktop.
Save miharekar/c99c57883ba44a00dd36bea71caf3f9d to your computer and use it in GitHub Desktop.
Starts with benchmark
require 'benchmark/ips'
Benchmark.ips do |x|
records = []
10_000.times do |i|
records << "#{['abc', 'rec', 'air'].sample}-#{i}-#{['abc', 'rec', 'air'].sample}-#{i}"
end
AIRTABLE_ID_REGEX = /rec(?!_)/.freeze
BETTER_AIRTABLE_ID_REGEX = /^rec(?!_)/.freeze
x.report("regex") do |_times|
selected = records.select do |record|
record =~ AIRTABLE_ID_REGEX
end
end
x.report("better regex") do |_times|
selected = records.select do |record|
record =~ BETTER_AIRTABLE_ID_REGEX
end
end
x.report("start with") do |_times|
selected = records.select do |record|
record.start_with?('rec')
end
end
# Compare the iterations per second of the various reports!
x.compare!
end
Warming up --------------------------------------
regex 30.469B i/100ms
better regex 31.843B i/100ms
start with 127.372B i/100ms
Calculating -------------------------------------
regex 8.547T (± 5.3%) i/s - 42.627T in 5.002786s
better regex 8.853T (± 6.1%) i/s - 44.039T in 5.000347s
start with 147.412T (± 6.1%) i/s - 734.042T in 4.998314s
Comparison:
start with: 147412033742104.2 i/s
better regex: 8852860871996.6 i/s - 16.65x (± 0.00) slower
regex: 8546672631009.6 i/s - 17.25x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment