Created
September 29, 2014 15:40
-
-
Save olivierlacan/4034cd788c26a0e88b57 to your computer and use it in GitHub Desktop.
Does it really make sense to optimize for a 1.01x speed improvement by using single-quoted strings instead of double-quoted Strings in Ruby (to avoid interpolation scanning in the double-quoted strings)? I think the answer is no.
This file contains hidden or 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
| $ ruby string_benchmark.rb | |
| Calculating ------------------------------------- | |
| single quote 128800 i/100ms | |
| double quote 128351 i/100ms | |
| ------------------------------------------------- | |
| single quote 6144842.6 (±5.4%) i/s - 30654400 in 5.008373s | |
| double quote 6089768.4 (±6.4%) i/s - 30290836 in 5.000430s | |
| Comparison: | |
| single quote: 6144842.6 i/s | |
| double quote: 6089768.4 i/s - 1.01x slower |
This file contains hidden or 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
| require 'benchmark/ips' | |
| Benchmark.ips do |x| | |
| x.report("single quote") { '' } | |
| x.report("double quote") { "" } | |
| x.compare! | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@olivierlacan 1% is well within the margin of error. I bet if you ran it a few more times, double quotes would seem faster. They’re the same.