From 0b815d8d1e4d0872a4f5aefbaf22990466f18712 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 12 Jun 2026 10:26:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(debug):=20=E6=B7=BB=E5=8A=A0=E5=86=85?= =?UTF-8?q?=E5=AD=98=E6=B3=84=E6=BC=8F=E6=A3=80=E6=B5=8B=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了随机数生成函数 gen_random - 添加了堆排序算法实现 heapsort 和 heapsort_r - 集成了高精度时间测量功能 gethrtime - 创建了性能测试框架包括 start_test 和 end_test 函数 - 实现了测试结果显示和统计功能 total - 添加了主函数 main 来执行完整的性能测试流程 --- debug/mem_leak.php | 102 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 debug/mem_leak.php diff --git a/debug/mem_leak.php b/debug/mem_leak.php new file mode 100644 index 00000000..ac161ba3 --- /dev/null +++ b/debug/mem_leak.php @@ -0,0 +1,102 @@ +> 1) + 1; + $ir = $n; + + while (1) { + if ($l > 1) { + $rra = $ra[--$l]; + } else { + $rra = $ra[$ir]; + $ra[$ir] = $ra[1]; + if (--$ir == 1) { + $ra[1] = $rra; + return; + } + } + $i = $l; + $j = $l << 1; + while ($j <= $ir) { + if (($j < $ir) && ($ra[$j] < $ra[$j+1])) { + $j++; + } + if ($rra < $ra[$j]) { + $ra[$i] = $ra[$j]; + $j += ($i = $j); + } else { + $j = $ir + 1; + } + } + $ra[$i] = $rra; + } +} + +function heapsort(int $N) { + global $LAST; + + define("IM", 139968); + define("IA", 3877); + define("IC", 29573); + + $LAST = 42; + for ($i=1; $i<=$N; $i++) { + $ary[$i] = gen_random(1); + } + heapsort_r($N, $ary); + printf("%.10f\n", $ary[$N]); +} + +function gethrtime(): float +{ + $hrtime = hrtime(); + return (($hrtime[0] * 1000000000.0 + $hrtime[1]) / 1000000000.0); +} + +function start_test() +{ + ob_start(); + return gethrtime(); +} + +function end_test($start, $name) +{ + global $total; + $end = gethrtime(); + ob_end_clean(); + $total += ($end - $start); + $num = number_format($end - $start, 3); + $pad = str_repeat(" ", 24 - strlen($name) - strlen($num)); + + echo $name . $pad . $num . "\n"; + ob_start(); + return gethrtime(); +} + +function total() +{ + global $total; + $pad = str_repeat("-", 24); + echo $pad . "\n"; + $num = number_format($total, 3); + $pad = str_repeat(" ", 24 - strlen("Total") - strlen($num)); + echo "Total" . $pad . $num . "\n"; +} + +function main() +{ + if (function_exists("date_default_timezone_set")) { + date_default_timezone_set("UTC"); + } + + $t0 = $t = start_test(); + heapsort(20000); + $t = end_test($t, "heapsort(20000)"); + total(); +}