Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Last active October 16, 2017 11:00
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 tagomoris/179a6a9d5beebf0de3d994007ecd9f7e to your computer and use it in GitHub Desktop.
Save tagomoris/179a6a9d5beebf0de3d994007ecd9f7e to your computer and use it in GitHub Desktop.
class X
NAME1 = "name1"
NAME2 = "name2"
NAME3 = "name3"
def replace_name_0(record, name)
record
end
def replace_name_1(record, name)
if record[NAME1] == NAME1
record[NAME1] = name
end
if record[NAME2] == NAME2
record[NAME2] = name
end
if record[NAME3] == NAME3
record[NAME3] = name
end
record
end
def replace_name_2(record, name)
k = NAME1
if record[k] == k
record[k] = name
end
k = NAME2
if record[k] == k
record[k] = name
end
k = NAME3
if record[k] == k
record[k] = name
end
record
end
def replace_name_3(record, name)
if record["name1"] == "name1"
record["name1"] = name
end
if record["name2"] == "name2"
record["name2"] = name
end
if record["name3"] == "name3"
record["name3"] = name
end
record
end
def replace_name_4(record, name)
if record["name1".freeze] == "name1".freeze
record["name1".freeze] = name
end
if record["name2".freeze] == "name2".freeze
record["name2".freeze] = name
end
if record["name3".freeze] == "name3".freeze
record["name3".freeze] = name
end
record
end
end
record1 = {}
record2 = {"name1" => "name1", "name2" => "name2", "name3" => "name3"}
name = "n"
loop_times = 1_000_000
x = X.new
st1 = Time.now
loop_times.times do |i|
x.replace_name_0(record1.dup, name)
end
score1 = Time.now - st1
# p(here: "replace_name_0 with blank ", time: score1)
st2 = Time.now
loop_times.times do |i|
x.replace_name_0(record2.dup, name)
end
score2 = Time.now - st2
# p(here: "replace_name_0 with record", time: score2)
st3 = Time.now
loop_times.times do |i|
x.replace_name_1(record1.dup, name)
end
score3 = Time.now - st3
p(here: "replace_name_1 with blank ", time: (score3 - score1).to_s[0,6])
st4 = Time.now
loop_times.times do |i|
x.replace_name_1(record2.dup, name)
end
score4 = Time.now - st4
p(here: "replace_name_1 with record", time: (score4 - score2).to_s[0,6])
st5 = Time.now
loop_times.times do |i|
x.replace_name_2(record1.dup, name)
end
score5 = Time.now - st5
p(here: "replace_name_2 with blank ", time: (score5 - score1).to_s[0,6])
st6 = Time.now
loop_times.times do |i|
x.replace_name_2(record2.dup, name)
end
score6 = Time.now - st6
p(here: "replace_name_2 with record", time: (score6 - score2).to_s[0,6])
st7 = Time.now
loop_times.times do |i|
x.replace_name_3(record1.dup, name)
end
score7 = Time.now - st7
p(here: "replace_name_3 with blank ", time: (score7 - score1).to_s[0,6])
st8 = Time.now
loop_times.times do |i|
x.replace_name_3(record2.dup, name)
end
score8 = Time.now - st7
p(here: "replace_name_3 with record", time: (score8 - score2).to_s[0,6])
st9 = Time.now
loop_times.times do |i|
x.replace_name_4(record1.dup, name)
end
score9 = Time.now - st9
p(here: "replace_name_4 with blank ", time: (score9 - score1).to_s[0,6])
st10 = Time.now
loop_times.times do |i|
x.replace_name_4(record2.dup, name)
end
score10 = Time.now - st10
p(here: "replace_name_4 with record", time: (score10 - score2).to_s[0,6])
MBA:bigdam-gateway tagomoris$ ruby ./hoge.rb
{:here=>"replace_name_1 with blank ", :time=>"0.0983"}
{:here=>"replace_name_1 with record", :time=>"0.6832"}
{:here=>"replace_name_2 with blank ", :time=>"0.1498"}
{:here=>"replace_name_2 with record", :time=>"0.5717"}
{:here=>"replace_name_3 with blank ", :time=>"0.1860"}
{:here=>"replace_name_3 with record", :time=>"1.0350"}
{:here=>"replace_name_4 with blank ", :time=>"0.1115"}
{:here=>"replace_name_4 with record", :time=>"0.4143"}
MBA:bigdam-gateway tagomoris$ h2o/mruby/host/bin/mruby ./hoge.rb
{:here=>"replace_name_1 with blank ", :time=>"0.5597"}
{:here=>"replace_name_1 with record", :time=>"0.8001"}
{:here=>"replace_name_2 with blank ", :time=>"0.4623"}
{:here=>"replace_name_2 with record", :time=>"0.8256"}
{:here=>"replace_name_3 with blank ", :time=>"0.8391"}
{:here=>"replace_name_3 with record", :time=>"2.4793"}
{:here=>"replace_name_4 with blank ", :time=>"1.0277"}
{:here=>"replace_name_4 with record", :time=>"1.6011"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment