Skip to content

Instantly share code, notes, and snippets.

@rightgo09
Created May 11, 2015 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightgo09/59184cb4210f0c771917 to your computer and use it in GitHub Desktop.
Save rightgo09/59184cb4210f0c771917 to your computer and use it in GitHub Desktop.
<?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";
$ 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
<?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