-
-
Save djbender/5877596 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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