yield w php

<?php

// Starting clock time in seconds
$start_time = microtime(true);
$a = 1;

function convert($size) {
	$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
	return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}

function getLines($file) {
	while ($line = fgets($file)) {
		yield $line;
	}
}

$file = fopen('a.csv', 'rb');
$lines = getLines($file);

foreach ($lines as $line) {
	//echo $line;
}
fclose($file);

echo convert(memory_get_usage(true)); // 123 kb

echo '<br>';
// End clock time in seconds
$end_time = microtime(true);

// Calculate script execution time
$execution_time = ($end_time - $start_time);

echo " Execution time of script = " . $execution_time . " sec";
<?php
// Starting clock time in seconds
$start_time = microtime(true);
$a = 1;

function convert($size) {
	$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
	return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}

function getLines($file) {
	$lines = [];
	while ($line = fgets($file)) {
		$lines[] = $line;
	}
	return $lines;
}

$file = fopen('a.csv', 'rb');
$lines = getLines($file);
fclose($file);

foreach ($lines as $line) {

}

echo convert(memory_get_usage(true)); // 123 kb

echo '<br>';
// End clock time in seconds
$end_time = microtime(true);

// Calculate script execution time
$execution_time = ($end_time - $start_time);

echo " Execution time of script = " . $execution_time . " sec";
Komentarze wyłączone