Created
May 11, 2015 12:16
-
-
Save rightgo09/59184cb4210f0c771917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$start_time=microtime(true); | |
$sum = 0; | |
foreach (range(0, 1000000) as $val) { | |
$sum += $val; | |
} | |
echo $sum.PHP_EOL; | |
$end_time = microtime(true); | |
echo "time: ", bcsub($end_time, $start_time, 4), "\n"; | |
echo "memory (byte): ", memory_get_peak_usage(true), "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php range_bench.php | |
500000500000 | |
time: 0.2835 | |
memory (byte): 144965632 | |
$ php xrange_bench.php | |
500000500000 | |
time: 0.8537 | |
memory (byte): 262144 | |
$ php -v | |
PHP 5.6.6 (cli) (built: Feb 20 2015 22:47:58) | |
Copyright (c) 1997-2015 The PHP Group | |
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies | |
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies | |
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$start_time = microtime(true); | |
$sum = 0; | |
function xrange($start, $end) { | |
for ($i = $start; $i <= $end; $i++) { | |
yield $i; | |
} | |
} | |
foreach (xrange(0, 1000000) as $val) { | |
$sum += $val; | |
} | |
echo $sum.PHP_EOL; | |
$end_time = microtime(true); | |
echo "time: ", bcsub($end_time, $start_time, 4), "\n"; | |
echo "memory (byte): ", memory_get_peak_usage(true), "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment