Skip to content

Instantly share code, notes, and snippets.

@haileys
Created June 27, 2013 14:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save haileys/5876809 to your computer and use it in GitHub Desktop.
Save haileys/5876809 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
HELLO_WORLD = "hello world".freeze
def self.make_lots_of_cheap_strings_the_traditional_way
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
b.report "traditional cheap strings" do
A.make_lots_of_cheap_strings_the_traditional_way
end
end
# Rehearsal -------------------------------------------------------------
# strings 2.410000 0.000000 2.410000 ( 2.413642)
# cheap strings 1.350000 0.000000 1.350000 ( 1.345134)
# traditional cheap strings 0.970000 0.000000 0.970000 ( 0.968080)
# ---------------------------------------------------- total: 4.730000sec
#
# user system total real
# strings 2.370000 0.010000 2.380000 ( 2.375827)
# cheap strings 1.340000 0.000000 1.340000 ( 1.338522)
# traditional cheap strings 0.970000 0.000000 0.970000 ( 0.968473)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment