Skip to content

Instantly share code, notes, and snippets.

@clemens
Created July 25, 2008 12:40
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 clemens/2434 to your computer and use it in GitHub Desktop.
Save clemens/2434 to your computer and use it in GitHub Desktop.
Object.class_eval do
def deep_clone
Marshal::load(Marshal.dump(self))
end
end
Array.class_eval do
def sum
self.inject(0) { |sum, element| sum += element }
end
def avg
sum/size
end
end
def benchmark(runs=1000, &block)
results = []
runs.times do
r0 = Time.now
yield
r1 = Time.now
results << r1.to_f - r0.to_f
end
results.avg
end
a = (1..99999).to_a
t1 = benchmark { b = a.deep_clone }
t2 = benchmark { b = a.dup }
puts t1 - t2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment