Created
February 29, 2012 03:34
-
-
Save rexbutler/1937381 to your computer and use it in GitHub Desktop.
Pair benchmark #1
This file contains 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
require "benchmark" | |
class Pair | |
attr_accessor :x,:y | |
def initialize(x,y) | |
@x = x | |
@y = y | |
end | |
end | |
def create_obj_pairs | |
$obj_pairs = [] | |
0.upto(10**6) do | |
$obj_pairs << Pair.new(0,0) | |
end | |
end | |
def create_hash_pairs | |
$hash_pairs = [] | |
0.upto(10**6) do | |
$hash_pairs = {:x => 0, :y => 0} | |
end | |
end | |
def create_array_pairs | |
$array_pairs = [] | |
0.upto(10**6) do | |
$array_pairs << [0,0] | |
end | |
end | |
def write_obj_pairs | |
0.upto(10**6) do |idx| | |
$obj_pairs[idx].x = 0 | |
$obj_pairs[idx].y = 0 | |
end | |
end | |
def write_hash_pairs | |
0.upto(10**6) do |idx| | |
$hash_pairs[:x] = 0 | |
$hash_pairs[:y] = 0 | |
end | |
end | |
def write_array_pairs | |
0.upto(10**6) do |idx| | |
$array_pairs[0] = 0 | |
$array_pairs[1] = 0 | |
end | |
end | |
def test_create_speeds | |
Benchmark.bmbm do |x| | |
x.report("Create Array Pairs: ") { create_array_pairs } | |
x.report("Create Hash Pairs: ") { create_hash_pairs } | |
x.report("Create Object Pairs: ") { create_obj_pairs } | |
end | |
end | |
def test_write_speeds | |
Benchmark.bmbm do |x| | |
x.report("Write Array Pairs: ") { write_array_pairs } | |
x.report("Write Hash Pairs: ") { write_hash_pairs } | |
x.report("Write Object Pairs: ") { write_obj_pairs } | |
end | |
end | |
test_create_speeds | |
test_write_speeds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment