Blog
It's a Wonderful Life
排序算法效率测试
针对五种不同的排序算法进行简单的效率测试,代码如下:
<?php
require_once("bubble_sort.php");
require_once("insert_sort.php");
require_once("select_sort.php");
require_once("quick_sort.php");
require_once("merge_sort.php");
for($i = 0; $i < 10000; $i++)
{
$arr[$i] = $i;
}
function time_test($sort)
{
global $arr;
shuffle($arr);
$start = microtime(true);
$arr = $sort($arr);
$end = microtime(true);
$time = $end - $start;
echo "$sort uses $time seconds to sort <br>";
}
time_test("bubble_sort");
time_test("insert_sort");
time_test("select_sort");
time_test("quick_sort");
time_test("divide");
输出结果类似于:
bubble_sort uses 5.5261211395264 seconds to sort
insert_sort uses 1.8885219097137 seconds to sort
select_sort uses 2.8068640232086 seconds to sort
quick_sort uses 0.025605916976929 seconds to sort
divide uses 0.29349398612976 seconds to sort