Created
October 2, 2010 23:44
-
-
Save bil-bas/608114 to your computer and use it in GitHub Desktop.
benchmarking color
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
| require 'benchmark' | |
| require 'gosu' | |
| n = 1000000 | |
| a1 = [1.0, 1.0, 1.0, 1.0] | |
| a255 = [255, 255, 255, 255] | |
| class Color | |
| attr_accessor :red, :blue, :green, :alpha | |
| def initialize(r, g, b, a) | |
| @red, @blue, @red, @green = r, g, b, a | |
| end | |
| end | |
| class ColorWrap | |
| def red; @data[0]; end | |
| def red=(value); @data[0] = value; end | |
| def initialize(*args) | |
| @data = args | |
| end | |
| end | |
| gosu = Gosu::Color.new(255,255,255,255) | |
| c = Color.new(1.0, 1.0, 1.0, 1.0) | |
| cw = nil | |
| Benchmark.bmbm do |bm| | |
| bm.report("float array#initialize") { n.times { a1 = [1.0, 1.0, 1.0, 1.0] } } | |
| bm.report("float array#[]") { n.times { a1[3] } } | |
| bm.report("float array#at") { n.times { a1.at(3) } } | |
| bm.report("float array#[]=0.0") { n.times { a1[3] = 0.0 } } | |
| bm.report("fixnum array#initialize") { n.times { a255 = [255, 255, 255, 255] } } | |
| bm.report("fixnum array#[]") { n.times { a255[3] } } | |
| bm.report("fixnum array#at") { n.times { a255.at(3) } } | |
| bm.report("fixnum array#[]=255") { n.times { a255[3] = 255 } } | |
| bm.report("float color#initialize") { n.times { c = Color.new(1.0, 1.0, 1.0, 1.0) } } | |
| bm.report("float color#red") { n.times { c.red } } | |
| bm.report("float color#red=0.0") { n.times { c.red = 0.0 } } | |
| bm.report("float colorwrap#initialize") { n.times { cw = ColorWrap.new(1.0, 1.0, 1.0, 1.0) } } | |
| bm.report("float colorwrap#red") { n.times { cw.red } } | |
| bm.report("float colorwrap#red=0.0") { n.times { cw.red = 0.0 } } | |
| bm.report("Gosu::Color#initialize") { n.times { gosu = Gosu::Color.new(255,255,255,255) } } | |
| bm.report("Gosu::Color#red") { n.times { gosu.red } } | |
| bm.report("Gosu::Color#red=0") { n.times { gosu.red = 0 } } | |
| end |
Author
Author
1.8.7
user system total real
float array#initialize 0.561000 0.000000 0.561000 ( 0.559000)
float array#[] 0.234000 0.000000 0.234000 ( 0.234000)
float array#at 0.234000 0.000000 0.234000 ( 0.231000)
float array#[]=0.0 0.265000 0.000000 0.265000 ( 0.261000)
fixnum array#initialize 0.546000 0.000000 0.546000 ( 0.557000)
fixnum array#[] 0.218000 0.000000 0.218000 ( 0.231000)
fixnum array#at 0.234000 0.000000 0.234000 ( 0.229000)
fixnum array#[]=255 0.265000 0.000000 0.265000 ( 0.256000)
float color#initialize 2.761000 0.062000 2.823000 ( 2.821000)
float color#red 0.202000 0.000000 0.202000 ( 0.199000)
float color#red=0.0 0.234000 0.000000 0.234000 ( 0.247000)
float colorwrap#initialize 1.887000 0.000000 1.887000 ( 1.890000)
float colorwrap#red 0.421000 0.000000 0.421000 ( 0.425000)
float colorwrap#red=0.0 0.514000 0.000000 0.514000 ( 0.521000)
Gosu::Color#initialize 3.526000 0.047000 3.573000 ( 3.569000)
Gosu::Color#red 0.219000 0.000000 0.219000 ( 0.208000)
Gosu::Color#red=0 0.296000 0.000000 0.296000 ( 0.291000)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.9.2
user system total real
float array#initialize 0.343000 0.000000 0.343000 ( 0.356000)
float array#[] 0.125000 0.000000 0.125000 ( 0.121000)
float array#at 0.172000 0.000000 0.172000 ( 0.174000)
float array#[]=0.0 0.218000 0.000000 0.218000 ( 0.210000)