相关疑难解决方法(0)

获取数组元素的索引比O(n)更快

鉴于我有一个巨大的数组,以及它的值.我想得到数组中的值的索引.还有其他方式,而不是打电话Array#index来获得它吗?问题来自需要保持真正庞大的阵列和Array#index大量的时间.

经过几次尝试后,我发现通过存储带有字段而不是值本身的结构来缓存元素内部的索引(value, index)会给性能带来巨大的进步(20倍的胜利).

我仍然想知道是否有更方便的方法来查找en元素的索引而不进行缓存(或者有一个很好的缓存技术可以提高性能).

ruby arrays indexing performance

103
推荐指数
4
解决办法
11万
查看次数

如果获取索引,如果数组中的对象包含Ruby中的期望值

所以这是我从某个地方获得的数组,我想得到包含"text"=>"Assets"的对象的索引.我怎么能在Ruby中做到这一点?

[{
                     "class" => "android.support.v7.widget.AppCompatTextView",
                       "tag" => nil,
               "description" => "android.support.v7.widget.AppCompatTextView{b777d8c V.ED..... ........ 0,39-355,168 #7f0e007f app:id/textview_sectiontitle}",
                        "id" => "textview_sectiontitle",
                      "text" => "Assets",
                   "visible" => true,
                      "rect" => {
              "height" => 129,
               "width" => 355,
                   "y" => 2088,
                   "x" => 80,
            "center_x" => 257,
            "center_y" => 2152
        },
                   "enabled" => true,
        "contentDescription" => nil
    },
    {
                     "class" => "android.support.v7.widget.AppCompatImageButton",
                       "tag" => nil,
               "description" => "android.support.v7.widget.AppCompatImageButton{ae57704 VFED..C.. ........ 1056,0-1280,208 #7f0e0109 app:id/imagebutton_amount}",
                        "id" => "imagebutton_amount",
                   "visible" => true,
                      "rect" => {
              "height" …
Run Code Online (Sandbox Code Playgroud)

ruby

1
推荐指数
1
解决办法
58
查看次数

标签 统计

ruby ×2

arrays ×1

indexing ×1

performance ×1