Created
August 14, 2017 20:43
-
-
Save elliotlarson/df5b9c90bfd8ea8b12e0565db311458c to your computer and use it in GitHub Desktop.
Benchmarking class creation vs hash
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/ips' | |
| class FooBar | |
| attr_accessor :foo, :bar | |
| def initialize(foo:, bar:) | |
| self.foo = foo | |
| self.bar = bar | |
| end | |
| end | |
| Benchmark.ips do |b| | |
| b.report('creating a class') do | |
| foobar = FooBar.new(foo: 'foo', bar: 'bar') | |
| end | |
| b.report('creating a hash') do | |
| foobar = { foo: 'foo', bar: 'bar' } | |
| end | |
| b.compare! | |
| end | |
| # Warming up -------------------------------------- | |
| # creating a class 71.215k i/100ms | |
| # creating a hash 133.030k i/100ms | |
| # Calculating ------------------------------------- | |
| # creating a class 836.255k (± 4.7%) i/s - 4.202M in 5.036732s | |
| # creating a hash 1.950M (± 4.3%) i/s - 9.844M in 5.057570s | |
| # Comparison: | |
| # creating a hash: 1950208.3 i/s | |
| # creating a class: 836255.1 i/s - 2.33x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment