Performance of Arrays and Hashes in Ruby

后端 未结 4 1426
旧巷少年郎
旧巷少年郎 2020-11-29 18:39

I have a program that will store many instances of one class, let\'s say up to 10.000 or more. The class instances have several properties that I need from time to time, but

4条回答
  •  星月不相逢
    2020-11-29 19:23

    When using unique values, you can use the Ruby Set which has been previously mentioned. Here are benchmark results. It's slightly slower than the hash though.

                     user     system      total        real
    array        0.460000   0.000000   0.460000 (  0.460666)
    hash         0.000000   0.000000   0.000000 (  0.000219)
    set          0.000000   0.000000   0.000000 (  0.000273)
    

    I simply added to @steenslag's code which can be found here https://gist.github.com/rsiddle/a87df54191b6b9dfe7c9.

    I used ruby 2.1.1p76 for this test.

提交回复
热议问题