Created
October 8, 2014 13:42
-
-
Save huacnlee/c7b7a14a4607a49a8bad to your computer and use it in GitHub Desktop.
String start_with? vs [0] ==
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
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 |
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
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