Last active
February 27, 2025 10:22
-
-
Save inouire/fa6821c8584d4937144122ed8c2628a8 to your computer and use it in GitHub Desktop.
Benchmark snippet for code perfo
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
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