[#43857] Hashへの生成順は保障されないのか? — Hiroshi Kasamatsu <qqmn89yb9@...>

こんにちは、笠松と申します。

88 messages 2007/08/18
[#43858] Re: Hashへの生成順は保障されないのか? — Urabe Shyouhei <shyouhei@...> 2007/08/18

Hiroshi Kasamatsu wrote:

[#43862] Re: Hashへの生成順は保障されないのか? — Hiroshi Kasamatsu <qqmn89yb9@...> 2007/08/19

皆さん、早速のレスありがとうございます。

[#43863] Re: Hashへの生成順は保障されないのか? — Urabe Shyouhei <shyouhei@...> 2007/08/19

Hiroshi Kasamatsu wrote:

[#43870] Re: Hashへの生成順は保障されないのか? — Hiroshi Kasamatsu <qqmn89yb9@...> 2007/08/20

Urabeさん、笠松です。レスありがとうございます。

[#43872] Re: Hashへの生成順は保障されないのか? — Urabe Shyouhei <shyouhei@...> 2007/08/20

Hiroshi Kasamatsu wrote:

[#43873] Re: Hashへの生成順は保障されないのか? — cuzic <cuzic@...> 2007/08/20

cuzic です。

[#43874] Re: Hashへの生成順は保障されないのか? — Urabe Shyouhei <shyouhei@...> 2007/08/20

cuzic wrote:

[#43875] Re: Hashへの生成順は保障されないのか? — Tanaka Akira <akr@...> 2007/08/20

In article <46C9E7BB.4060100@ruby-lang.org>,

[#43876] Re: Hashへの生成順は保障されないのか? — Urabe Shyouhei <shyouhei@...> 2007/08/20

おお、田中さんを満足させる説明ってのは結構ハードル高そうだな。

[#43878] Re: Hashへの生成順は保障されないのか? — しん <dezawa@...> 2007/08/20

# 出遅れたので、レスすべきメールが判らなくなってしまったので、手近なのに

[#43879] Re: Hashへの生成順は保障されないのか? — Yukihiro Matsumoto <matz@...> 2007/08/20

まつもと ゆきひろです

[#43887] Re: Hashへの生成順は保障されないのか? — Nobuyoshi Nakada <nobu@...> 2007/08/21

なかだです。

[#43891] Re: Hashへの生成順は保障されないのか? — SASADA Koichi <ko1@...> 2007/08/21

 ささだです。

[#43892] Re: Hashへの生成順は保障されないのか? — Yukihiro Matsumoto <matz@...> 2007/08/21

まつもと ゆきひろです

[#43893] Re: Hashへの生成順は保障されないのか? — Nobuyoshi Nakada <nobu@...> 2007/08/21

なかだです。

[#43899] Re: Hashへの生成順は保障されないのか? — "Akinori MUSHA" <knu@...> 2007/08/21

At Tue, 21 Aug 2007 13:59:43 +0900,

[#43900] Re: Hashへの生成順は保障されないのか? — SASADA Koichi <ko1@...> 2007/08/21

 ささだです。

[#43906] Re: Hashへの生成順は保障されないのか? — "Akinori MUSHA" <knu@...> 2007/08/21

At Tue, 21 Aug 2007 19:29:11 +0900,

[#43921] Re: Hashへの生成順は保障されないのか? — Tanaka Akira <akr@...> 2007/08/22

In article <86sl6dgikh.knu@iDaemons.org>,

[#43926] Re: Hashへの生成順は保障されないのか? — Tanaka Akira <akr@...> 2007/08/23

In article <87zm0kaz60.fsf@fsij.org>,

[#43927] Re: Hashへの生成順は保障されないのか? — Yugui <yugui@...> 2007/08/24

Yuguiといいます。

[#43930] Re: Hashへの生成順は保障されないのか? — Yukihiro Matsumoto <matz@...> 2007/08/24

まつもと ゆきひろです

[ruby-list:43887] Re: Hashへの生成順は保障されないのか?

From: Nobuyoshi Nakada <nobu@...>
Date: 2007-08-21 01:43:25 UTC
List: ruby-list #43887
なかだです。

At Tue, 21 Aug 2007 07:46:20 +0900,
Yukihiro Matsumoto wrote in [ruby-list:43879]:
> 実は私自身はHashの順序の保存については積極的に反対しているわ
> けではないのです。しかし、
> 
>   * 一般的にはHashの順序が保存されない
>   * どの順序が最適かは実は一意に決まらない(ような気がする)

「どの順序」と選べるほど選択肢はないような気がするのですが。

>   * 順序の保存によってHashの効率が下がるのはうれしくない

[ruby-dev:24569]を1.9最新用に直して、Hal Fultonから送られてきた
ベンチマークを試してみました。

http://www.rubyist.net/~nobu/ruby/ordered-hash-1.9.diff
http://www.rubyist.net/~nobu/ruby/ordered-hash-1.8.diff

予想通りdeleteがやや遅くなっていますが、思ったほどの影響はないよ
うです。逆にeachやsort_byが速くなっているのは、二重配列から単純
なdouble linked listをたどるようになっているのが大きいのかもしれ
ません。

前
add to empty hash                         0.650000   0.010000   0.660000 (  0.654691)
add to hash with 100000 elements          0.710000   0.020000   0.730000 (  0.730761)
each_key with 200000 elements             0.050000   0.000000   0.050000 (  0.053228)
keys.sort_by.each, 200000 elements        1.150000   0.000000   1.150000 (  1.159369)
delete 100000 times, non-existing key     0.040000   0.000000   0.040000 (  0.034815)
delete 100000 times, existing keys        0.260000   0.000000   0.260000 (  0.256023)
include? 100000 times, non-existing key   0.050000   0.000000   0.050000 (  0.046105)
include? 100000 times, existing keys      0.230000   0.000000   0.230000 (  0.225641)
clear hash with 100000 elements           0.030000   0.000000   0.030000 (  0.037633)

後
                                              user     system      total        real
add to empty hash                         0.550000   0.020000   0.570000 (  0.558828)
add to hash with 100000 elements          0.620000   0.010000   0.630000 (  0.636749)
each_key with 200000 elements             0.040000   0.000000   0.040000 (  0.043783)
keys.sort_by.each, 200000 elements        0.270000   0.010000   0.280000 (  0.276364)
delete 100000 times, non-existing key     0.030000   0.000000   0.030000 (  0.035522)
delete 100000 times, existing keys        0.270000   0.000000   0.270000 (  0.265942)
include? 100000 times, non-existing key   0.030000   0.000000   0.030000 (  0.028704)
include? 100000 times, existing keys      0.190000   0.000000   0.190000 (  0.193877)
clear hash with 100000 elements           0.040000   0.000000   0.040000 (  0.035513)


-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

Attachments (1)

bm_hash.rb (1.26 KB, text/x-ruby)
# Hash benchmarking suite
# by Ryan Leavengood

require 'benchmark'

class SymbolGen
  def initialize(value='aaaaaaaa')
    @value = value
  end

  def generate
    result = @value.intern
    @value = @value.succ
    result
  end
end

if $0 == __FILE__
  sg = SymbolGen.new
  h = {}
  iterations = 100000
  Benchmark.bm(40) do |bx|
    block = proc { h[sg.generate] = sg.generate }
    bx.report("add to empty hash") { iterations.times &block } 
    bx.report("add to hash with #{iterations} elements") { iterations.times &block } 
    bx.report("each_key with #{2*iterations} elements") { h.each_key {|key|} } 
    bx.report("keys.sort_by.each, #{2*iterations} elements") { h.keys.sort_by{|k| k.to_s}.each {|key|} } 
    bx.report("delete #{iterations} times, non-existing key") { iterations.times { h.delete(:not_a_key) } }
    sg2 = SymbolGen.new
    bx.report("delete #{iterations} times, existing keys") { iterations.times { h.delete(sg2.generate); sg2.generate } }
    bx.report("include? #{iterations} times, non-existing key") { iterations.times { h.include?(:not_a_key) } }
    bx.report("include? #{iterations} times, existing keys") { iterations.times { h.include?(sg2.generate); sg2.generate } }
    bx.report("clear hash with #{iterations} elements") { h.clear }
  end
end

In This Thread