Created
August 13, 2017 16:23
-
-
Save watzon/d3f3b7d53a5aba569c8f75b7a7c935e5 to your computer and use it in GitHub Desktop.
Crystal Benchmark - gsub vs split & join
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
require "benchmark" | |
Benchmark.ips do |x| | |
str = "this is text with too many spaces in it" | |
x.report("gsub") do | |
500.times { str = str.gsub(/\s+/, " ") } | |
end | |
x.report("split/join") do | |
500.times { str = str.split().join(" ") } | |
end | |
end |
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
❯ ./gsub-vs-split_join | |
gsub 970.48 ( 1.03ms) (± 4.95%) 1.41× slower | |
split/join 1.37k (730.13µs) (± 3.20%) fastest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment