使用静态类方法来*快速*数组排序通过在PHP键值 [英] Using a Static Class Method to *Quickly* Sort an Array By Key Value in PHP

查看:117
本文介绍了使用静态类方法来*快速*数组排序通过在PHP键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题比别人不同,因为它的重点是对数组排序与静态类方法,而不是典型的程序性做法。

我找实现以下功能sortByKeyValue非常高性能的​​方式。其他有些相关答案都集中在完成工作,这个问题更多的是完成工作,并得到它非常快速地完成(作为一个静态方法)。

任何人想借此裂缝吧?我可能会扔在这个问题上的一些赏金挤出性能迷们。 :)

 < PHP$数据=阵列(
   阵列('名'=>'B','俗气'=>'培根'),
   阵列('名'=>'C','美味'=>'炸玉米饼'),
   阵列('名'=>'A','可爱'=>'viddles'),
);MYARRAY类{    公共静态函数sortByKeyValue($数组$关键,$方向='ASC'){
        //做的事
        // 帮我!        返回$阵列;
    }}$数据= MYARRAY :: sortByKeyValue($的数据,'名');//应该输出以名称键
//我不包括垃圾钥匙我扔了,但他们应该在那里太)
// [{名:A},{名:B},{名:C}]
?>


解决方案

我跑到下面来比较 multisort 的速度,pre-PHP 5.3的方法,与使用更现代的方法 usort 与闭合功能:

  $阿尔法='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ CNT = 1000;
$关键='名';
$方向='ASC';
$阵列=阵列();
为($ I = 0; $ I< $ CNT; $ I ++){
    $阵列[$ i] ['名'] = SUBSTR(str_shuffle($阿尔法),0,8);
    $阵列[$ i] ['工作'] = SUBSTR(str_shuffle($阿尔法),0,8);
}
$pre = $阵列; //测试假人// pre-PHP 5.3$ T [0] = -microtime();
$子=阵列();
的foreach($pre $作为项目){
        $子[] = $项目[$关键];
}
如果($方向=='ASC')$ ORD =的SORT_ASC;
否则$ ORD = SORD_DESC;
在array_multisort($子,$ ORD,$pre);
$ T [0] + = microtime中();// USORT与闭幕$ T [1] = -microtime();
usort($阵列功能($ A,$ B)使用($键,$方向){
    如果($方向=='ASC'){
        返回STRCMP($ A [$键],$ B [$关键]);
    }
    返回STRCMP($ B [$键],$ A [$关键]);
});
$ T [1] + = microtime中();
后续代码var_dump($ T);

正如你所看到的,老的方法是快两倍以上:

 阵列

    [0] => 0.005
    [1] => 0.014001

因此​​,这里是我会怎么做的类:

 类MYARRAY {    公共静态函数sortByKeyValue($数组$关键,$ =方向SORT_ASC){
        $子=阵列();
        的foreach($数组作为$项目){
            $子[] = $项目[$关键];
        }
        在array_multisort($子,$方向,$阵列);
        返回$阵列;
    }}

This question is different than others, as it's focus is on sorting an array with a static class method rather than the typical procedural approach.

I am look for a very performant way to implement the function sortByKeyValue below. Other somewhat related answers are focused on getting the job done, this question is more about getting the job done and getting it done very quickly (as a static method).

Anyone want to take a crack at it? I'll probably throw some bounty on this question to squeeze out the performance junkies. :)

<?php

$data = array(
   array('name' => 'B', 'cheesy' => 'bacon'),
   array('name' => 'C', 'delicious' => 'tacos'),
   array('name' => 'A', 'lovely' => 'viddles'),
);

class MyArray {

    public static function sortByKeyValue($array, $key, $direction = 'ASC') {
        // Do the thing
        // Help me!

        return $array;
    }

}

$data = MyArray::sortByKeyValue($data, 'name');

// Should output the name key in order
// I am not including the garbage keys I threw in, but they should be there too)
// [{"name": "A"},{"name": "B"},{"name": "C"}]
?>

解决方案

I ran the following to compare the speed of multisort, a pre-PHP 5.3 method, with a more modern method that uses usort with a closure function:

$alpha = 'abcdefghijklmnopqrstuvwxyz';
$cnt = 1000;
$key = 'name';
$direction = 'ASC';
$array = array();
for ($i=0; $i<$cnt; $i++){
    $array[$i]['name'] = substr(str_shuffle($alpha), 0, 8);
    $array[$i]['job'] = substr(str_shuffle($alpha), 0, 8);
}
$pre = $array;//the test dummies

//PRE-PHP 5.3

$t[0] = -microtime();
$sub = array();
foreach ($pre as $item) {
        $sub[] = $item[$key];
}
if ($direction == 'ASC') $ord = SORT_ASC;
else $ord = SORD_DESC;
array_multisort($sub, $ord, $pre);
$t[0] += microtime();

//USORT WITH CLOSURE

$t[1] = -microtime();
usort($array, function ($a, $b) use($key, $direction){
    if ($direction == 'ASC'){
        return strcmp($a[$key], $b[$key]);
    }
    return strcmp($b[$key], $a[$key]);
});
$t[1] += microtime();
var_dump($t);

As you can see, the old method was more than twice as fast:

Array
(
    [0] => 0.005
    [1] => 0.014001
)

So here's how I would do the class:

class MyArray {

    public static function sortByKeyValue($array, $key, $direction = SORT_ASC) {
        $sub = array();
        foreach ($array as $item) {
            $sub[] = $item[$key];
        }
        array_multisort($sub, $direction, $array);
        return $array;
    }

}

这篇关于使用静态类方法来*快速*数组排序通过在PHP键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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