Skip to content

Instantly share code, notes, and snippets.

@samleb
Last active August 29, 2015 13:57
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 samleb/9455926 to your computer and use it in GitHub Desktop.
Save samleb/9455926 to your computer and use it in GitHub Desktop.
Comparative benchmark of `String#squeeze` and `String#gsub` Attribute Normalizer's `SquishNormalizer`
require 'benchmark'
$iterations = 10_000
$str1 = "Hi guys. What are you doing tonight ?"
$str2 = " "
puts RUBY_DESCRIPTION
puts
[$str1, $str2].each do |string|
puts "Benchmarking with #{string.inspect}"
puts
Benchmark.bmbm do |x|
x.report("gsub:") { $iterations.times { string.gsub(/\s+/, ' ') } }
x.report("squeeze:") { $iterations.times { string.squeeze(' ') } }
end
puts
end
ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-darwin13.1.0]
Benchmarking with "Hi guys. What are you doing tonight ?"
Rehearsal --------------------------------------------
gsub: 0.080000 0.000000 0.080000 ( 0.086052)
squeeze: 0.020000 0.000000 0.020000 ( 0.015867)
----------------------------------- total: 0.100000sec
user system total real
gsub: 0.080000 0.000000 0.080000 ( 0.075941)
squeeze: 0.010000 0.000000 0.010000 ( 0.011751)
Benchmarking with " "
Rehearsal --------------------------------------------
gsub: 0.050000 0.000000 0.050000 ( 0.049008)
squeeze: 0.010000 0.000000 0.010000 ( 0.019988)
----------------------------------- total: 0.060000sec
user system total real
gsub: 0.050000 0.010000 0.060000 ( 0.047103)
squeeze: 0.020000 0.000000 0.020000 ( 0.011867)
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.0.0]
Benchmarking with "Hi guys. What are you doing tonight ?"
Rehearsal --------------------------------------------
gsub: 0.070000 0.000000 0.070000 ( 0.070130)
squeeze: 0.010000 0.000000 0.010000 ( 0.012903)
----------------------------------- total: 0.080000sec
user system total real
gsub: 0.060000 0.000000 0.060000 ( 0.062146)
squeeze: 0.010000 0.000000 0.010000 ( 0.010956)
Benchmarking with " "
Rehearsal --------------------------------------------
gsub: 0.040000 0.000000 0.040000 ( 0.050117)
squeeze: 0.020000 0.000000 0.020000 ( 0.015469)
----------------------------------- total: 0.060000sec
user system total real
gsub: 0.050000 0.000000 0.050000 ( 0.047934)
squeeze: 0.010000 0.000000 0.010000 ( 0.010047)
jruby 1.7.9 (1.9.3p392) 2013-12-06 87b108a on Java HotSpot(TM) 64-Bit Server VM 1.6.0_65-b14-462-11M4609 [darwin-x86_64]
Benchmarking with "Hi guys. What are you doing tonight ?"
Rehearsal --------------------------------------------
gsub: 0.930000 0.010000 0.940000 ( 0.555000)
squeeze: 0.240000 0.000000 0.240000 ( 0.128000)
----------------------------------- total: 1.180000sec
user system total real
gsub: 0.230000 0.000000 0.230000 ( 0.134000)
squeeze: 0.010000 0.000000 0.010000 ( 0.005000)
Benchmarking with " "
Rehearsal --------------------------------------------
gsub: 0.040000 0.000000 0.040000 ( 0.045000)
squeeze: 0.010000 0.000000 0.010000 ( 0.005000)
----------------------------------- total: 0.050000sec
user system total real
gsub: 0.260000 0.000000 0.260000 ( 0.144000)
squeeze: 0.010000 0.000000 0.010000 ( 0.005000)
rubinius 2.2.5.n68 (2.1.0 d19d76f7 2014-03-09 JI) [x86_64-darwin13.1.0]
Benchmarking with "Hi guys. What are you doing tonight ?"
Rehearsal --------------------------------------------
gsub: 0.480002 0.010612 0.490614 ( 0.284320)
squeeze: 0.363016 0.004568 0.367584 ( 0.198975)
----------------------------------- total: 0.858198sec
user system total real
gsub: 0.363769 0.005412 0.369181 ( 0.201671)
squeeze: 0.361539 0.003563 0.365102 ( 0.195498)
Benchmarking with " "
Rehearsal --------------------------------------------
gsub: 0.070952 0.000603 0.071555 ( 0.038430)
squeeze: 0.199274 0.002251 0.201525 ( 0.114850)
----------------------------------- total: 0.273080sec
user system total real
gsub: 0.074753 0.000545 0.075298 ( 0.044644)
squeeze: 0.131669 0.001201 0.132870 ( 0.079878)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment