Skip to content

Instantly share code, notes, and snippets.

@ainame
Forked from takai/1. uniq_bench.rb
Created May 9, 2012 17:58
Show Gist options
  • Select an option

  • Save ainame/2647349 to your computer and use it in GitHub Desktop.

Select an option

Save ainame/2647349 to your computer and use it in GitHub Desktop.

Revisions

  1. Satoshi Namai revised this gist May 9, 2012. 2 changed files with 26 additions and 26 deletions.
    4 changes: 2 additions & 2 deletions 1. uniq_bench.rb
    Original file line number Diff line number Diff line change
    @@ -24,13 +24,13 @@

    x.report('set') do
    sources.each do |source|
    Set.new(source.dup)
    Set.new(source)
    end
    end

    x.report('hash') do
    sources.each do |source|
    source.dup.each_with_object(Hash.new) do|i, h| h[i] end.keys end
    source.each_with_object(Hash.new) do|i, h| h[i] end.keys end
    end

    #破壊的な操作なので最後
    48 changes: 24 additions & 24 deletions MacbookPro 13inc 2.3GHz(Early 2011)を利用
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,36 @@
    "max: 100"
    user system total real
    uniq 0.890000 0.000000 0.890000 ( 0.904162)
    set 2.370000 0.000000 2.370000 ( 2.393207)
    hash 1.350000 0.010000 1.360000 ( 1.353670)
    uniq! 0.920000 0.010000 0.930000 ( 0.941576)
    uniq 0.910000 0.010000 0.920000 ( 0.918938)
    set 2.410000 0.000000 2.410000 ( 2.419544)
    hash 1.350000 0.000000 1.350000 ( 1.357918)
    uniq! 0.910000 0.010000 0.920000 ( 0.919179)
    "max: 1000"
    user system total real
    uniq 1.020000 0.000000 1.020000 ( 1.029936)
    set 2.560000 0.010000 2.570000 ( 2.572257)
    hash 1.380000 0.000000 1.380000 ( 1.385801)
    uniq! 1.030000 0.020000 1.050000 ( 1.053234)
    uniq 1.010000 0.000000 1.010000 ( 1.007825)
    set 2.460000 0.010000 2.470000 ( 2.486916)
    hash 1.390000 0.010000 1.400000 ( 1.391193)
    uniq! 0.980000 0.000000 0.980000 ( 0.987927)
    "max: 10000"
    user system total real
    uniq 1.660000 0.010000 1.670000 ( 1.673859)
    set 3.070000 0.020000 3.090000 ( 3.106747)
    hash 1.330000 0.010000 1.340000 ( 1.339507)
    uniq! 1.850000 0.020000 1.870000 ( 1.889848)
    uniq 1.620000 0.000000 1.620000 ( 1.630699)
    set 3.330000 0.040000 3.370000 ( 3.383102)
    hash 1.470000 0.000000 1.470000 ( 1.473038)
    uniq! 1.650000 0.010000 1.660000 ( 1.669997)
    "max: 100000"
    user system total real
    uniq 5.110000 0.060000 5.170000 ( 5.224048)
    set 5.630000 0.160000 5.790000 ( 5.812479)
    hash 1.360000 0.010000 1.370000 ( 1.365014)
    uniq! 4.640000 0.050000 4.690000 ( 4.732754)
    uniq 4.650000 0.060000 4.710000 ( 4.723563)
    set 5.750000 0.170000 5.920000 ( 5.954741)
    hash 1.390000 0.000000 1.390000 ( 1.401017)
    uniq! 4.560000 0.070000 4.630000 ( 4.646408)
    "max: 1000000"
    user system total real
    uniq 6.150000 0.090000 6.240000 ( 6.267968)
    set 7.510000 0.270000 7.780000 ( 7.807583)
    hash 1.350000 0.000000 1.350000 ( 1.360542)
    uniq! 6.330000 0.110000 6.440000 ( 6.459583)
    uniq 6.190000 0.090000 6.280000 ( 6.309521)
    set 7.580000 0.270000 7.850000 ( 7.880618)
    hash 1.430000 0.000000 1.430000 ( 1.443003)
    uniq! 6.260000 0.110000 6.370000 ( 6.393830)
    "max: 10000000"
    user system total real
    uniq 6.230000 0.100000 6.330000 ( 6.351809)
    set 7.560000 0.280000 7.840000 ( 7.872657)
    hash 1.370000 0.000000 1.370000 ( 1.381345)
    uniq! 6.570000 0.100000 6.670000 ( 6.685479)
    uniq 6.510000 0.090000 6.600000 ( 6.632788)
    set 8.040000 0.310000 8.350000 ( 8.379355)
    hash 1.350000 0.000000 1.350000 ( 1.359664)
    uniq! 6.660000 0.120000 6.780000 ( 6.800269)
  2. Satoshi Namai revised this gist May 9, 2012. 2 changed files with 31 additions and 31 deletions.
    2 changes: 1 addition & 1 deletion 1. uniq_bench.rb
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    end
    end

    p "max_value: #domaxend"
    p "max: #{max}"

    Benchmark.bm(5) do |x|
    x.report('uniq') do
    60 changes: 30 additions & 30 deletions MacbookPro 13inc 2.3GHz(Early 2011)を利用
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,36 @@
    "max_value: 100"
    "max: 100"
    user system total real
    uniq 0.960000 0.000000 0.960000 ( 0.970205)
    set 2.760000 0.010000 2.770000 ( 2.788782)
    hash 1.580000 0.000000 1.580000 ( 1.586609)
    uniq! 1.090000 0.020000 1.110000 ( 1.120790)
    "max_value: 1000"
    uniq 0.890000 0.000000 0.890000 ( 0.904162)
    set 2.370000 0.000000 2.370000 ( 2.393207)
    hash 1.350000 0.010000 1.360000 ( 1.353670)
    uniq! 0.920000 0.010000 0.930000 ( 0.941576)
    "max: 1000"
    user system total real
    uniq 1.170000 0.010000 1.180000 ( 1.186608)
    set 3.020000 0.010000 3.030000 ( 3.046173)
    hash 1.560000 0.000000 1.560000 ( 1.571826)
    uniq! 1.140000 0.020000 1.160000 ( 1.165698)
    "max_value: 10000"
    uniq 1.020000 0.000000 1.020000 ( 1.029936)
    set 2.560000 0.010000 2.570000 ( 2.572257)
    hash 1.380000 0.000000 1.380000 ( 1.385801)
    uniq! 1.030000 0.020000 1.050000 ( 1.053234)
    "max: 10000"
    user system total real
    uniq 1.580000 0.000000 1.580000 ( 1.591290)
    set 3.090000 0.030000 3.120000 ( 3.128032)
    hash 1.360000 0.000000 1.360000 ( 1.363069)
    uniq! 1.660000 0.020000 1.680000 ( 1.693592)
    "max_value: 100000"
    uniq 1.660000 0.010000 1.670000 ( 1.673859)
    set 3.070000 0.020000 3.090000 ( 3.106747)
    hash 1.330000 0.010000 1.340000 ( 1.339507)
    uniq! 1.850000 0.020000 1.870000 ( 1.889848)
    "max: 100000"
    user system total real
    uniq 4.510000 0.060000 4.570000 ( 4.581352)
    set 5.580000 0.170000 5.750000 ( 5.769500)
    hash 1.320000 0.000000 1.320000 ( 1.334091)
    uniq! 4.610000 0.060000 4.670000 ( 4.692547)
    "max_value: 1000000"
    uniq 5.110000 0.060000 5.170000 ( 5.224048)
    set 5.630000 0.160000 5.790000 ( 5.812479)
    hash 1.360000 0.010000 1.370000 ( 1.365014)
    uniq! 4.640000 0.050000 4.690000 ( 4.732754)
    "max: 1000000"
    user system total real
    uniq 6.220000 0.090000 6.310000 ( 6.320836)
    set 7.870000 0.270000 8.140000 ( 8.226232)
    hash 1.360000 0.010000 1.370000 ( 1.364586)
    uniq! 6.170000 0.090000 6.260000 ( 6.291180)
    "max_value: 10000000"
    uniq 6.150000 0.090000 6.240000 ( 6.267968)
    set 7.510000 0.270000 7.780000 ( 7.807583)
    hash 1.350000 0.000000 1.350000 ( 1.360542)
    uniq! 6.330000 0.110000 6.440000 ( 6.459583)
    "max: 10000000"
    user system total real
    uniq 6.260000 0.100000 6.360000 ( 6.378876)
    set 7.530000 0.280000 7.810000 ( 7.838983)
    hash 1.410000 0.000000 1.410000 ( 1.414521)
    uniq! 6.500000 0.110000 6.610000 ( 6.633919)
    uniq 6.230000 0.100000 6.330000 ( 6.351809)
    set 7.560000 0.280000 7.840000 ( 7.872657)
    hash 1.370000 0.000000 1.370000 ( 1.381345)
    uniq! 6.570000 0.100000 6.670000 ( 6.685479)
  3. Satoshi Namai revised this gist May 9, 2012. 2 changed files with 61 additions and 32 deletions.
    44 changes: 34 additions & 10 deletions 1. uniq_bench.rb
    Original file line number Diff line number Diff line change
    @@ -2,18 +2,42 @@
    require 'benchmark'
    require 'set'

    # 1回の配列の生成に対して100回の平均をとる
    # 100種類の乱数の配列のデータを取る
    n = 100
    # 配列の要素の最大値が100...100000まで比較
    [100, 1000, 10000, 100000, 1000000, 10000000].each do |max|
    # 5回のベンチの平均
    # 毎回異なる要素数100000個の乱数の配列に対して100回操作
    sources = n.times.map do
    100000.times.map do
    rand(max)
    end
    end

    p "max_value: #domaxend"

    Benchmark.bm(5) do |x|
    # 要素数100000個の乱数の配列に対して操作
    source = 100000.times.map { rand(max) }
    p "max_value: #{max}"
    x.report('uniq') do n.times { source.dup.uniq.size } end
    x.report('uniq!') do n.times { source.dup.uniq! } end
    x.report('set') do n.times { Set.new(source.dup) } end
    x.report('hash') do n.times { source.dup.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
    x.report('uniq') do
    sources.each do |source|
    source.uniq
    end
    end

    x.report('set') do
    sources.each do |source|
    Set.new(source.dup)
    end
    end

    x.report('hash') do
    sources.each do |source|
    source.dup.each_with_object(Hash.new) do|i, h| h[i] end.keys end
    end

    #破壊的な操作なので最後
    x.report('uniq!') do
    sources.each do |source|
    source.uniq!
    end
    end
    end
    end
    end
    49 changes: 27 additions & 22 deletions MacbookPro 13inc 2.3GHz(Early 2011)を利用
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,36 @@
    user system total real
    "max_value: 100"
    uniq 0.950000 0.000000 0.950000 ( 0.950976)
    uniq! 0.890000 0.010000 0.900000 ( 0.902435)
    set 2.380000 0.000000 2.380000 ( 2.386007)
    hash 1.350000 0.000000 1.350000 ( 1.355174)
    user system total real
    uniq 0.960000 0.000000 0.960000 ( 0.970205)
    set 2.760000 0.010000 2.770000 ( 2.788782)
    hash 1.580000 0.000000 1.580000 ( 1.586609)
    uniq! 1.090000 0.020000 1.110000 ( 1.120790)
    "max_value: 1000"
    uniq 1.010000 0.010000 1.020000 ( 1.020505)
    uniq! 1.060000 0.000000 1.060000 ( 1.064673)
    set 2.630000 0.010000 2.640000 ( 2.653748)
    hash 1.440000 0.000000 1.440000 ( 1.441658)
    user system total real
    uniq 1.170000 0.010000 1.180000 ( 1.186608)
    set 3.020000 0.010000 3.030000 ( 3.046173)
    hash 1.560000 0.000000 1.560000 ( 1.571826)
    uniq! 1.140000 0.020000 1.160000 ( 1.165698)
    "max_value: 10000"
    uniq 1.710000 0.000000 1.710000 ( 1.724164)
    uniq! 1.680000 0.000000 1.680000 ( 1.683601)
    set 3.270000 0.020000 3.290000 ( 3.289946)
    hash 1.470000 0.000000 1.470000 ( 1.470825)
    user system total real
    uniq 1.580000 0.000000 1.580000 ( 1.591290)
    set 3.090000 0.030000 3.120000 ( 3.128032)
    hash 1.360000 0.000000 1.360000 ( 1.363069)
    uniq! 1.660000 0.020000 1.680000 ( 1.693592)
    "max_value: 100000"
    uniq 4.500000 0.080000 4.580000 ( 4.581424)
    uniq! 4.690000 0.080000 4.770000 ( 4.781564)
    set 5.630000 0.100000 5.730000 ( 5.735537)
    hash 1.490000 0.010000 1.500000 ( 1.491445)
    user system total real
    uniq 4.510000 0.060000 4.570000 ( 4.581352)
    set 5.580000 0.170000 5.750000 ( 5.769500)
    hash 1.320000 0.000000 1.320000 ( 1.334091)
    uniq! 4.610000 0.060000 4.670000 ( 4.692547)
    "max_value: 1000000"
    user system total real
    uniq 6.220000 0.090000 6.310000 ( 6.320836)
    set 7.870000 0.270000 8.140000 ( 8.226232)
    hash 1.360000 0.010000 1.370000 ( 1.364586)
    uniq! 6.170000 0.090000 6.260000 ( 6.291180)
    "max_value: 10000000"
    uniq 6.980000 0.130000 7.110000 ( 7.143206)
    uniq! 7.040000 0.130000 7.170000 ( 7.198300)
    set 8.300000 0.180000 8.480000 ( 8.512272)
    hash 1.500000 0.000000 1.500000 ( 1.505083)
    user system total real
    uniq 6.260000 0.100000 6.360000 ( 6.378876)
    set 7.530000 0.280000 7.810000 ( 7.838983)
    hash 1.410000 0.000000 1.410000 ( 1.414521)
    uniq! 6.500000 0.110000 6.610000 ( 6.633919)
  4. Satoshi Namai revised this gist May 9, 2012. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions MacbookPro 13inc 2.3GHz(Early 2011)を利用
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    user system total real
    "max_value: 100"
    uniq 0.950000 0.000000 0.950000 ( 0.950976)
    uniq! 0.890000 0.010000 0.900000 ( 0.902435)
    set 2.380000 0.000000 2.380000 ( 2.386007)
    hash 1.350000 0.000000 1.350000 ( 1.355174)
    user system total real
    "max_value: 1000"
    uniq 1.010000 0.010000 1.020000 ( 1.020505)
    uniq! 1.060000 0.000000 1.060000 ( 1.064673)
    set 2.630000 0.010000 2.640000 ( 2.653748)
    hash 1.440000 0.000000 1.440000 ( 1.441658)
    user system total real
    "max_value: 10000"
    uniq 1.710000 0.000000 1.710000 ( 1.724164)
    uniq! 1.680000 0.000000 1.680000 ( 1.683601)
    set 3.270000 0.020000 3.290000 ( 3.289946)
    hash 1.470000 0.000000 1.470000 ( 1.470825)
    user system total real
    "max_value: 100000"
    uniq 4.500000 0.080000 4.580000 ( 4.581424)
    uniq! 4.690000 0.080000 4.770000 ( 4.781564)
    set 5.630000 0.100000 5.730000 ( 5.735537)
    hash 1.490000 0.010000 1.500000 ( 1.491445)
    user system total real
    "max_value: 10000000"
    uniq 6.980000 0.130000 7.110000 ( 7.143206)
    uniq! 7.040000 0.130000 7.170000 ( 7.198300)
    set 8.300000 0.180000 8.480000 ( 8.512272)
    hash 1.500000 0.000000 1.500000 ( 1.505083)

  5. Satoshi Namai revised this gist May 9, 2012. 3 changed files with 19 additions and 24 deletions.
    31 changes: 19 additions & 12 deletions 1. uniq_bench.rb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,19 @@
    require 'benchmark'
    require 'set'

    source = 100000.times.map { rand(100) }

    n = 100
    Benchmark.bm(5) do |x|
    x.report('uniq') do n.times { source.uniq } end
    x.report('uniq!') do n.times { source.uniq! } end
    x.report('set') do n.times { Set.new(source) } end
    x.report('hash') do n.times { source.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
    end
    # -*- coding: utf-8 -*-
    require 'benchmark'
    require 'set'

    # 1回の配列の生成に対して100回の平均をとる
    n = 100
    # 配列の要素の最大値が100...100000まで比較
    [100, 1000, 10000, 100000, 1000000, 10000000].each do |max|
    # 5回のベンチの平均
    Benchmark.bm(5) do |x|
    # 要素数100000個の乱数の配列に対して操作
    source = 100000.times.map { rand(max) }
    p "max_value: #{max}"
    x.report('uniq') do n.times { source.dup.uniq.size } end
    x.report('uniq!') do n.times { source.dup.uniq! } end
    x.report('set') do n.times { Set.new(source.dup) } end
    x.report('hash') do n.times { source.dup.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
    end
    end
    6 changes: 0 additions & 6 deletions 2. ruby 1.9.3p125
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
    user system total real
    uniq 0.770000 0.000000 0.770000 ( 0.777292)
    uniq! 0.010000 0.000000 0.010000 ( 0.011109)
    set 0.010000 0.000000 0.010000 ( 0.003713)
    hash 0.000000 0.000000 0.000000 ( 0.001277)
    6 changes: 0 additions & 6 deletions 3. jruby 1.6.7
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_31) [darwin-x86_64-java]
    user system total real
    uniq 0.555000 0.000000 0.555000 ( 0.555000)
    uniq! 0.015000 0.000000 0.015000 ( 0.015000)
    set 0.208000 0.000000 0.208000 ( 0.208000)
    hash 0.089000 0.000000 0.089000 ( 0.090000)
  6. @takai takai revised this gist Apr 26, 2012. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  7. @takai takai revised this gist Apr 26, 2012. 4 changed files with 16 additions and 9 deletions.
    5 changes: 0 additions & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    user system total real
    uniq 0.760000 0.000000 0.760000 ( 0.761028)
    uniq! 0.010000 0.000000 0.010000 ( 0.009451)
    set 0.010000 0.000000 0.010000 ( 0.005617)
    hash 0.000000 0.000000 0.000000 ( 0.001273)
    6 changes: 6 additions & 0 deletions jruby 1.6.7
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_31) [darwin-x86_64-java]
    user system total real
    uniq 0.555000 0.000000 0.555000 ( 0.555000)
    uniq! 0.015000 0.000000 0.015000 ( 0.015000)
    set 0.208000 0.000000 0.208000 ( 0.208000)
    hash 0.089000 0.000000 0.089000 ( 0.090000)
    6 changes: 6 additions & 0 deletions ruby 1.9.3p125
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
    user system total real
    uniq 0.770000 0.000000 0.770000 ( 0.777292)
    uniq! 0.010000 0.000000 0.010000 ( 0.011109)
    set 0.010000 0.000000 0.010000 ( 0.003713)
    hash 0.000000 0.000000 0.000000 ( 0.001277)
    8 changes: 4 additions & 4 deletions uniq_bench.rb
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@

    n = 100
    Benchmark.bm(5) do |x|
    x.report(:uniq) do n.times { source.uniq } end
    x.report(:uniq!) do n.times { source.uniq! } end
    x.report(:set) do n.times { Set.new(source) } end
    x.report(:hash) do n.times { source.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
    x.report('uniq') do n.times { source.uniq } end
    x.report('uniq!') do n.times { source.uniq! } end
    x.report('set') do n.times { Set.new(source) } end
    x.report('hash') do n.times { source.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
    end
  8. @takai takai created this gist Apr 26, 2012.
    5 changes: 5 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    user system total real
    uniq 0.760000 0.000000 0.760000 ( 0.761028)
    uniq! 0.010000 0.000000 0.010000 ( 0.009451)
    set 0.010000 0.000000 0.010000 ( 0.005617)
    hash 0.000000 0.000000 0.000000 ( 0.001273)
    12 changes: 12 additions & 0 deletions uniq_bench.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    require 'benchmark'
    require 'set'

    source = 100000.times.map { rand(100) }

    n = 100
    Benchmark.bm(5) do |x|
    x.report(:uniq) do n.times { source.uniq } end
    x.report(:uniq!) do n.times { source.uniq! } end
    x.report(:set) do n.times { Set.new(source) } end
    x.report(:hash) do n.times { source.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
    end