Skip to content

Instantly share code, notes, and snippets.

@inouire
Last active February 27, 2025 10:22
Show Gist options
  • Save inouire/fa6821c8584d4937144122ed8c2628a8 to your computer and use it in GitHub Desktop.
Save inouire/fa6821c8584d4937144122ed8c2628a8 to your computer and use it in GitHub Desktop.
Benchmark snippet for code perfo
def regex(str) = str =~ /^https?:\/\//i
def starts_with(str)
str.start_with?("http://") || str.start_with?("https://")
end
strings = [
"https://example.com",
"http://example.com",
"example1",
"example2"
]
require "benchmark/ips"
Benchmark.ips do |x|
x.report("regex") { strings.each { regex(it) } }
x.report("starts_with") { strings.each { starts_with(it) } }
x.compare!
end
# credit to User stephannv for this snippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment