<?php $arrayCount = 2000; $cycleCount = 100000; echo "向函数传递参数并做修改", PHP_EOL; function ByVal($p){return $p[intval(count($p) / 2)] += 1;} function ByRef(&$p){return $p[intval(count($p) / 2)] += 1;} $p = array_fill(0, $arrayCount, 1); $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $a = ByVal($p); } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByValFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $a = ByRef($p); } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByRefFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL; echo "向函数传递参数不做修改", PHP_EOL; function ByVal2($p){return $p[intval(count($p) / 2)];} function ByRef2(&$p){return $p[intval(count($p) / 2)];} $p = array_fill(0, $arrayCount, 1); $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $a = ByVal2($p); } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByValFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $a = ByRef2($p); } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByRefFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL; echo "变量赋值做修改", PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); $a = $p; $a[2] += 1; } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByVal: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); $a = &$p; $a[2] += 1; } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL; echo "变量赋值不做修改", PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); $a = $p; $b = $a[2]; } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByVal: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); $a = &$p; $b = $a[2]; } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL; echo "数组内循环赋值修改数据", PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); foreach($p as $key => $val){ $p[$key] += 1; } } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByKey: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); foreach($p as &$val){ $val += 1; } } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL; echo "数组内循环赋值不修改数据", PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); foreach($p as $key => $val){ $b = $val; } } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByKey: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL; $start = microtime(true); $startMem = Memory_get_usage(); for($i = 0; $i < $cycleCount; $i += 1){ $p = array_fill(0, $arrayCount, 1); foreach($p as &$val){ $b = $val; } } $end = microtime(true); $endMem = Memory_get_usage(); echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;