Created
September 12, 2012 22:46
-
-
Save kwatch/3710544 to your computer and use it in GitHub Desktop.
PHP5.5 generator benchmark #1
This file contains hidden or 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 | |
/// | |
/// benchmark: loop vs array vs generator vs inner iterator vs closure | |
/// | |
function create_datafile($filename, $filesize=1024) { | |
$str = "Haruhi\tFemale\t16\tTeam Leader\n" | |
. "Mikuru\tFemale\t17\tTime Traveler\n" | |
. "Yuki\tFemale\t16\tHumanoid Interface\n" | |
. "Itsuki\tMale\t16\tESPer Body\n" | |
. "Kyon\tMale\t16\tStory Teller\n" | |
; | |
$f = fopen($filename, 'w'); | |
$size = 0; | |
do { | |
fwrite($f, $str); | |
$size += strlen($str); | |
} while ($size < $filesize); | |
fclose($f); | |
} | |
/// simple loop | |
function loop_lines($filename) { | |
$f = fopen($filename, 'r'); | |
while (($line = fgets($f)) !== false) { | |
//$arr = explode("\t", $line); | |
//var_dump($arr); | |
} | |
fclose($f); | |
} | |
/// array | |
function read_lines($filename) { | |
$f = fopen($filename, 'r'); | |
$lines = []; | |
while (($line = fgets($f)) !== false) { | |
$lines[] = $line; | |
} | |
fclose($f); | |
return $lines; | |
} | |
/// generator | |
function each_line($filename) { | |
$f = fopen($filename, 'r'); | |
while (($line = fgets($f)) !== false) { | |
yield $line; | |
} | |
fclose($f); | |
} | |
/// inner iterator | |
function do_each_line_with($filename, $block) { | |
$f = fopen($filename, 'r'); | |
while (($line = fgets($f)) !== false) { | |
$block($line); | |
} | |
fclose($f); | |
} | |
/// closure | |
function fetch_lines($filename) { | |
$f = fopen($filename, 'r'); | |
return function() use ($f) { | |
$line = fgets($f); | |
//// or | |
//if (($line = fgets($f)) === false) { | |
// fclose($f); | |
//} | |
return $line; | |
}; | |
} | |
/// Benchmark | |
function run_benchmark($title, $block) { | |
$begin = microtime(true); | |
$block(); | |
$end = microtime(true); | |
printf("%-15s %8.4f (sec)\n", $title, ($end - $begin)); | |
} | |
function run_benchmarks($filename) { | |
/// read file into disk cache | |
$f = fopen($filename, 'r'); | |
do { | |
$line = fgets($f); | |
} while ($line !== false); | |
fclose($f); | |
/// simple loop | |
run_benchmark("loop", function() use ($filename) { | |
loop_lines($filename); | |
}); | |
/// array | |
run_benchmark("array", function() use ($filename) { | |
foreach (read_lines($filename) as $line) { | |
//$arr = explode("\t", $line); | |
//var_dump($arr); | |
} | |
}); | |
/// generator | |
run_benchmark("generator", function() use ($filename) { | |
foreach (each_line($filename) as $line) { | |
//$arr = explode("\t", $line); | |
//var_dump($arr); | |
} | |
}); | |
/// inner iterator | |
run_benchmark("inner iterator", function() use ($filename) { | |
do_each_line_with($filename, function($line) { | |
//$arr = explode("\t", $line); | |
//var_dump($arr); | |
}); | |
}); | |
/// closure | |
run_benchmark("closure", function() use ($filename) { | |
$fetch = fetch_lines($filename); | |
while (($line = $fetch()) !== false) { | |
//$arr = explode("\t", $line); | |
//var_dump($arr); | |
} | |
}); | |
} | |
$filename = "example.data"; | |
if (! file_exists($filename)) { | |
create_datafile($filename, 15*1024*1024); | |
} | |
run_benchmarks($filename); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment