|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +$: << File.expand_path('../../lib', __FILE__) |
| 4 | + |
| 5 | +require 'benchmark' |
| 6 | +require 'concurrent' |
| 7 | + |
| 8 | +n = 500_000 |
| 9 | + |
| 10 | +StructPair = Struct.new(:left, :right) |
| 11 | +SafePair = Concurrent::MutableStruct.new(:left, :right) |
| 12 | +FinalPair = Concurrent::SettableStruct.new(:left, :right) |
| 13 | +ImmutablePair = Concurrent::ImmutableStruct.new(:left, :right) |
| 14 | + |
| 15 | +array_pair = [true, false].freeze |
| 16 | +struct_pair = StructPair.new(true, false) |
| 17 | +safe_pair = SafePair.new(true, false) |
| 18 | +final_pair = FinalPair.new(true, false) |
| 19 | +immutable = ImmutablePair.new(true, false) |
| 20 | + |
| 21 | +puts "Object creation...\n" |
| 22 | +Benchmark.bmbm do |x| |
| 23 | + x.report('create frozen array') { n.times{ [true, false].freeze } } |
| 24 | + x.report('create frozen struct') { n.times{ StructPair.new(true, false).freeze } } |
| 25 | + x.report('create mutable struct') { n.times{ SafePair.new(true, false) } } |
| 26 | + x.report('create settable struct') { n.times{ FinalPair.new(true, false) } } |
| 27 | + x.report('create immutable struct') { n.times{ ImmutablePair.new(true, false) } } |
| 28 | +end |
| 29 | + |
| 30 | +puts "\n" |
| 31 | + |
| 32 | +puts "Object access...\n" |
| 33 | +Benchmark.bmbm do |x| |
| 34 | + x.report('read from frozen array') { n.times{ array_pair.last } } |
| 35 | + x.report('read from frozen struct') { n.times{ struct_pair.right } } |
| 36 | + x.report('read from mutable struct') { n.times{ safe_pair.right } } |
| 37 | + x.report('read from settable struct') { n.times{ final_pair.right } } |
| 38 | + x.report('read from immutable struct') { n.times{ immutable.right } } |
| 39 | +end |
| 40 | + |
| 41 | +puts "\n" |
| 42 | + |
| 43 | +puts "Enumeration...\n" |
| 44 | +Benchmark.bmbm do |x| |
| 45 | + x.report('iterate over frozen array') { n.times{ array_pair.each{ nil } } } |
| 46 | + x.report('iterate over frozen struct') { n.times{ struct_pair.each{ nil } } } |
| 47 | + x.report('iterate over mutable struct') { n.times{ safe_pair.each{ nil } } } |
| 48 | + x.report('iterate over settable struct') { n.times{ final_pair.each{ nil } } } |
| 49 | + x.report('iterate over immutable struct') { n.times{ immutable.each{ nil } } } |
| 50 | +end |
| 51 | + |
| 52 | +__END__ |
| 53 | + |
| 54 | +Object creation... |
| 55 | +Rehearsal ----------------------------------------------------------- |
| 56 | +create frozen array 0.090000 0.000000 0.090000 ( 0.091262) |
| 57 | +create frozen struct 0.180000 0.000000 0.180000 ( 0.179993) |
| 58 | +create mutable struct 2.030000 0.000000 2.030000 ( 2.052071) |
| 59 | +create settable struct 2.070000 0.000000 2.070000 ( 2.080022) |
| 60 | +create immutable struct 0.710000 0.000000 0.710000 ( 0.716877) |
| 61 | +-------------------------------------------------- total: 5.080000sec |
| 62 | + |
| 63 | + user system total real |
| 64 | +create frozen array 0.100000 0.000000 0.100000 ( 0.097776) |
| 65 | +create frozen struct 0.190000 0.000000 0.190000 ( 0.186287) |
| 66 | +create mutable struct 2.020000 0.010000 2.030000 ( 2.032391) |
| 67 | +create settable struct 2.030000 0.000000 2.030000 ( 2.031631) |
| 68 | +create immutable struct 0.690000 0.000000 0.690000 ( 0.695010) |
| 69 | + |
| 70 | +Object access... |
| 71 | +Rehearsal -------------------------------------------------------------- |
| 72 | +read from frozen array 0.060000 0.000000 0.060000 ( 0.060430) |
| 73 | +read from frozen struct 0.060000 0.000000 0.060000 ( 0.058978) |
| 74 | +read from mutable struct 0.440000 0.000000 0.440000 ( 0.454071) |
| 75 | +read from settable struct 0.460000 0.000000 0.460000 ( 0.457699) |
| 76 | +read from immutable struct 0.120000 0.000000 0.120000 ( 0.126701) |
| 77 | +----------------------------------------------------- total: 1.140000sec |
| 78 | + |
| 79 | + user system total real |
| 80 | +read from frozen array 0.060000 0.000000 0.060000 ( 0.063006) |
| 81 | +read from frozen struct 0.060000 0.000000 0.060000 ( 0.094203) |
| 82 | +read from mutable struct 0.420000 0.000000 0.420000 ( 0.468304) |
| 83 | +read from settable struct 0.410000 0.000000 0.410000 ( 0.452446) |
| 84 | +read from immutable struct 0.110000 0.010000 0.120000 ( 0.127030) |
| 85 | + |
| 86 | +Enumeration... |
| 87 | +Rehearsal ----------------------------------------------------------------- |
| 88 | +iterate over frozen array 0.170000 0.000000 0.170000 ( 0.176898) |
| 89 | +iterate over frozen struct 0.160000 0.000000 0.160000 ( 0.160786) |
| 90 | +iterate over mutable struct 1.520000 0.000000 1.520000 ( 1.627013) |
| 91 | +iterate over settable struct 1.500000 0.010000 1.510000 ( 1.525163) |
| 92 | +iterate over immutable struct 0.990000 0.000000 0.990000 ( 1.006201) |
| 93 | +-------------------------------------------------------- total: 4.350000sec |
| 94 | + |
| 95 | + user system total real |
| 96 | +iterate over frozen array 0.170000 0.000000 0.170000 ( 0.167927) |
| 97 | +iterate over frozen struct 0.150000 0.000000 0.150000 ( 0.157328) |
| 98 | +iterate over mutable struct 1.450000 0.000000 1.450000 ( 1.462654) |
| 99 | +iterate over settable struct 1.460000 0.000000 1.460000 ( 1.480270) |
| 100 | +iterate over immutable struct 0.940000 0.010000 0.950000 ( 0.955633) |
0 commit comments