有效地对分隔的数字串进行排序,并重新排列相关的分隔字符串以匹配排序

时间:2012-05-22 10:37:58

标签: php performance sorting delimiter

我有三个字符串是由一个长时间的组合查询和文件读取过程生成的,每个字符串都与另一个相关。


示例:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

我想根据版本号按升序排序。

示例结果:

$versions = "1 2 3 4 5 6 7 8 9 10";
$weights = "50.2 60.5 70 10 35 60 60.1 50 75 15.98";
$ids = "512 318 943 326 112 191 961 977 441 155";

我的问题是:有没有比我目前更有效的方式呢? 请注意,这些字符串可能会变大,我见过的最大字符是~600个不同的版本

我执行以下操作:

  • 爆炸字符串
  • 复制版本号为密钥的数组
  • 按键排序数组
  • 内爆回字符串

以下是代码和a live example

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids = explode(" ", $ids);

$s_versions = array();
$s_weights = array();
$s_ids = array();

//set keys to correspond to version number
foreach($a_versions as $key => $ver){
    $s_versions[$ver] = $a_versions[$key];
    $s_weights[$ver] = $a_weights[$key];
    $s_ids[$ver] = $a_ids[$key];
}

//sort according to keys
ksort($s_versions, SORT_NUMERIC);
ksort($s_weights, SORT_NUMERIC);
ksort($s_ids, SORT_NUMERIC);

//implode back
$versions = implode(" ", $s_versions); 
$weights = implode(" ", $s_weights); 
$ids = implode(" ", $s_ids); 

echo "
    <pre>
        $versions
        $weights
        $ids
    </pre>
";

/*==========
Results
    1 2 3 4 5 6 7 8 9 10
    50.2 60.5 70 10 35 60 60.1 50 75 15.98
    512 318 943 326 112 191 961 977 441 155
==========*/

性能提升#1:

  

用array_combine替换foreach循环可能会带来一点性能提升。 - svens

确实如此,根据a simple unit test,它的速度提高了大约11-15%。

2 个答案:

答案 0 :(得分:1)

这是我能找到的最好的:

$versions = array_flip(explode(' ', $versions));
$weights = explode(' ', $weights);
$ids = explode(' ', $ids);

ksort($versions, SORT_NUMERIC);
foreach ($versions as $version => $idx) {
  $result[0][] = $version;
  $result[1][] = $weights[$idx];
  $result[2][] = $ids[$idx];
}

return array(
  implode(' ', $result[0]),
  implode(' ', $result[1]),
  implode(' ', $result[2]),
);

请参阅: http://codepad.viper-7.com/flcvnO 比较我测试的内容(包括你的初始代码和array_combine的代码)

答案 1 :(得分:1)

这是另一种方法。这是最简单的例子,没有键:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$version_weight_id = array_map(null, $a_versions, $a_weights, $a_ids);

print_r($version_weight_id);

如果你想要更多键控的东西,你必须有一个地图功能,如下所示:

function version_weight_id($v, $w, $id) {

    return array('version' => $v, 'weight' => $w, 'id' => $id);

}

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$version_weight_id = array_map('version_weight_id', $a_versions, $a_weights, $a_ids);

array_multisort($version_weight_id, $a_versions);

print_r($version_weight_id);

编辑:

这是另一种不需要地图功能的方法:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$weights_ids = array_map(null, $a_weights, $a_ids);
$versions_weights_ids = array_combine($a_versions, $weights_ids);

print_r($versions_weights_ids);

问题是你必须知道主键是版本,子阵列0键是权重,子阵列1键是id。

要打印出您的结果,请使用:

foreach($versions_weights_ids as $version => $weight_id) {
echo "
    <pre>
        $version
        {$weight_id[0]}
        {$weight_id[1]}
    </pre> ";
}

如果您正在寻找ajax解决方案,我会考虑使用JSON,并使用更多类似的内容:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$weights_ids = array_map(null, $a_weights, $a_ids);
$versions_weights_ids = array_combine($a_versions, $weights_ids);

echo json_encode($versions_weights_ids);

这样,您可以使用框架的每个方法或针对特定版本(或多个版本)进行输出。