挑一个哈希的第N个元素的最快方法 [英] the fastest way to pick the Nth element of a hash

查看:108
本文介绍了挑一个哈希的第N个元素的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的哈希表(字符串的数组索引),并寻找一个功能迅速的从中挑选出第一个(理想情况下,也第N个)元素。 array_shift()复位()是我的需要过于缓慢。

更新:我也不找了基于参考的解决方案,功能应该接受前pressions在 get_first(some_func_returning_array())

array_slice法(荣誉浓汤)似乎成为最后的赢家。完整的标杆code

 函数bigary($ N){
    $ A =阵列();
    $ S =范围('A','Z');
    做{
        洗牌($ S);
        美元[SUBSTR(破灭('',$ S),兰特(10,20))] = $ N;
    }而( - $ N);
    返回$一个;
}功能timeit($名称,$ FN){
    全球$结果;    $循环= 1000;
    $大小= 5432;    静态$一个;
    如果$ a = bigary($大小)($ A!);    $ T = microtime中(1);
    为($ I = 0; $ I< $循环; $ I ++)
        $ B = $ FN($ A);
    $结果[$名称] = microtime中(1) - $ T;
}timeit('假',函数($ A){
    //基准PHP函数调用开销
});timeit('array_shift',函数($ A){
    返回array_shift($ A);
});timeit('复位',函数($ A){
    返回复位($ A);
});timeit('的foreach',函数($ A){
    的foreach($ A $为b)退回​​$ B;
});timeit('键',函数($ A){
    $ B = array_keys($ A);
    返回$一个[$ B [0]];
});timeit('值',函数($ A){
    $ B = array_values​​($ A);
    返回$ B [0];
});timeit('片',函数($ A){
    $ B = array_slice($一个,0,1);
    返回复位($ B);
});ASORT($结果);的foreach($结果$名=> $时间)
    的printf(%20秒=%.3f \\ n,$名称,$时间);

结果:

 虚拟= 0.393
           片= 0.433
          值= 0.824
         的foreach = 0.929
           复位= 0.935
     array_shift = 0.954
            键= 1.371


解决方案

使用 array_slice 只获取的 N 的-th项目和 array_pop 终于得到它:

  $ nthItem = array_pop(array_slice($改编,$ N,1));

I've got a big hashtable (array with string indexes) and looking for a function that quickly picks the first (ideally, also Nth) element from it. array_shift() and reset() are too slow for my needs.

UPDATE: i'm also not looking for a reference-based solution, the function should accept expressions as in get_first(some_func_returning_array())

ANSWER array_slice method (kudos Gumbo) seems to be the winner. Complete benchmarking code

function bigary($n) {
    $a = array();
    $s = range('A', 'Z');
    do {
        shuffle($s);
        $a[substr(implode('', $s), rand(10, 20))] = $n;
    } while(--$n);
    return $a;
}

function timeit($name, $fn) {
    global $results;

    $loops = 1000;
    $size  = 5432;

    static $a;
    if(!$a) $a = bigary($size);

    $t = microtime(1);
    for($i = 0; $i < $loops; $i++)
        $b = $fn($a);
    $results[$name] = microtime(1) - $t;
}

timeit('dummy', function ($a) { 
    // benchmark php function call overhead
});

timeit('array_shift', function ($a) { 
    return array_shift($a); 
});

timeit('reset', function ($a) { 
    return reset($a); 
});

timeit('foreach', function ($a) { 
    foreach($a as $b) return $b;
});

timeit('keys', function ($a) { 
    $b = array_keys($a); 
    return $a[$b[0]];
});

timeit('values', function ($a) { 
    $b = array_values($a); 
    return $b[0];
});

timeit('slice', function ($a) { 
    $b = array_slice($a, 0, 1); 
    return reset($b);
});

asort($results);

foreach($results as $name => $time)
    printf("%20s = %.3f\n", $name, $time);

Results:

           dummy = 0.393
           slice = 0.433
          values = 0.824
         foreach = 0.929
           reset = 0.935
     array_shift = 0.954
            keys = 1.371

解决方案

Use array_slice to get an array of just the n-th item and array_pop to finally get it:

$nthItem = array_pop(array_slice($arr, $n, 1));

这篇关于挑一个哈希的第N个元素的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆