Skip to content

Instantly share code, notes, and snippets.

@watzon
Created August 13, 2017 16:23
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 watzon/d3f3b7d53a5aba569c8f75b7a7c935e5 to your computer and use it in GitHub Desktop.
Save watzon/d3f3b7d53a5aba569c8f75b7a7c935e5 to your computer and use it in GitHub Desktop.
Crystal Benchmark - gsub vs split & join
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
❯ ./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