Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Created October 8, 2014 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huacnlee/c7b7a14a4607a49a8bad to your computer and use it in GitHub Desktop.
Save huacnlee/c7b7a14a4607a49a8bad to your computer and use it in GitHub Desktop.
String start_with? vs [0] ==
Calculating -------------------------------------
str.start_with? 78491 i/100ms
str[0] == '/' 66410 i/100ms
-------------------------------------------------
str.start_with? 3663514.9 (±7.8%) i/s - 18209912 in 5.002869s
str[0] == '/' 2905683.6 (±7.6%) i/s - 14477380 in 5.011908s
Comparison:
str.start_with?: 3663514.9 i/s
str[0] == '/': 2905683.6 i/s - 1.26x slower
str = "/lkasdjglaskjdg"
Benchmark.ips do |x|
x.report("str.start_with?") {
str.start_with?('/')
}
x.report("str[0] == '/'") {
str[0] == '/'
}
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment