Skip to content

Instantly share code, notes, and snippets.

@andhapp
Forked from haileys/cheap_strings.rb
Created June 27, 2013 14:19
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 andhapp/5876806 to your computer and use it in GitHub Desktop.
Save andhapp/5876806 to your computer and use it in GitHub Desktop.
module CheapStrings
def `(str)
str
end
end
module A
extend CheapStrings
def self.make_lots_of_strings
10_000_000.times do
"hello world"
end
end
def self.make_lots_of_cheap_strings
10_000_000.times do
`hello world`
end
end
end
require "benchmark"
Benchmark.bmbm do |b|
b.report "strings" do
A.make_lots_of_strings
end
b.report "cheap strings" do
A.make_lots_of_cheap_strings
end
end
# λ ruby cheap_strings.rb
# Rehearsal -------------------------------------------------
# strings 2.390000 0.000000 2.390000 ( 2.389402)
# cheap strings 1.340000 0.000000 1.340000 ( 1.346113)
# ---------------------------------------- total: 3.730000sec
#
# user system total real
# strings 2.380000 0.000000 2.380000 ( 2.384954)
# cheap strings 1.350000 0.000000 1.350000 ( 1.350808)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment