The following function retrieves a line in a file, regardless of its size, so you won't get an error if the file's size is beyond php's allowed memory limit [the string has to be below however], which is something i was needing for accessing a big log file generated by a webhost. Indexes start at 1 [so $line = 1 means the first line unlike arrays]. If the file is small, it would be better to use "file[]" however.
function strpos_count[$haystack, $needle, $i = 0] {
while [strpos[$haystack,$needle] !== false] {$haystack = substr[$haystack, [strpos[$haystack,$needle] + 1]]; $i++;}
return $i;
}
function getLine[$file,$line=1]{
$occurence = 0;
$contents = '';
$startPos = -1;
if [!file_exists[$file]] return '';
$fp = @fopen[$file, "rb"];
if [!$fp] return '';
while [!@feof[$fp]] {
$str = @fread[$fp, 1024];
$number_of_occurences = strpos_count[$str,"\n"];
if [$number_of_occurences == 0] {if [$start_pos != -1] {$contents .= $str;}}
else {
$lastPos = 0;
for [$i = 0; $i < $number_of_occurences; $i++]{
$pos = strpos[$str,"\n", $lastPos];
$occurence++;
if [$occurence == $line] {
$startPos = $pos;
if [$i == $number_of_occurences - 1] {$contents = substr[$str, $startPos + 1];}
} elseif [$occurence == $line + 1] {
if [$i == 0] {$contents .= substr[$str, 0, $pos];} else {$contents = substr[$str, $startPos, $pos - $startPos];}
$occurence = 0;
break;
}
$lastPos = $pos + 1;
}
}
}
@fclose[$fp];
return $contents;
}
?>
When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory [in my experiments]. It would seem like this could be optimized in the language during runtime [if $var++ is the only thing in the whole statement, it could be treated as ++$var].
I conducted many tests [I believe to be fair], and here's one of the results:
$i++ took 8.47515535355 seconds and 2360 bytes
++$i took 7.80081486702 seconds and 2160 bytes
Here's my code. If anyone sees a bias in it, tell me. I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times [10 - 15 million uses of the ++ operator].
ini_set[ 'MAX_EXEC_TIME', 120 ];
ob_start[ ];
$num_tests = 10;
$startFirst = $startSecond = $endFirst = $endSecond = $startFirstMemory = $endFirstMemory = $startSecondMemory = $endSecondMemory = $someVal = 0;
$times = array[ '$i++' => array[ 'time' => 0, 'memory' => 0 ], '++$i' => array[ 'total' => 0, 'memory' => 0 ] ];
________số 8
for[ $i = 0, $startSecondMemory = memory_get_usage[ ], $startSecond = microtime[ true ]; $i < 10000000; ++$i ]{ $someval = 2; }
$endSecondMemory = memory_get_usage[ ];
$endSecond = microtime[ true ];
function strpos_count[$haystack, $needle, $i = 0] {
0
while [strpos[$haystack,$needle] !== false] {$haystack = substr[$haystack, [strpos[$haystack,$needle] + 1]]; $i++;}
return $i;
}
function getLine[$file,$line=1]{
$occurence = 0;
$contents = '';
$startPos = -1;
if [!file_exists[$file]] return '';
$fp = @fopen[$file, "rb"];
if [!$fp] return '';
while [!@feof[$fp]] {
$str = @fread[$fp, 1024];
$number_of_occurences = strpos_count[$str,"\n"];
if [$number_of_occurences == 0] {if [$start_pos != -1] {$contents .= $str;}}
else {
$lastPos = 0;
for [$i = 0; $i < $number_of_occurences; $i++]{
$pos = strpos[$str,"\n", $lastPos];
$occurence++;
if [$occurence == $line] {
$startPos = $pos;
if [$i == $number_of_occurences - 1] {$contents = substr[$str, $startPos + 1];}
} elseif [$occurence == $line + 1] {
if [$i == 0] {$contents .= substr[$str, 0, $pos];} else {$contents = substr[$str, $startPos, $pos - $startPos];}
$occurence = 0;
break;
}
$lastPos = $pos + 1;
}
}
}
@fclose[$fp];
return $contents;
}
?>
function strpos_count[$haystack, $needle, $i = 0] {
1
while [strpos[$haystack,$needle] !== false] {$haystack = substr[$haystack, [strpos[$haystack,$needle] + 1]]; $i++;}
return $i;
}
function getLine[$file,$line=1]{
$occurence = 0;
$contents = '';
$startPos = -1;
if [!file_exists[$file]] return '';
$fp = @fopen[$file, "rb"];
if [!$fp] return '';
while [!@feof[$fp]] {
$str = @fread[$fp, 1024];
$number_of_occurences = strpos_count[$str,"\n"];
if [$number_of_occurences == 0] {if [$start_pos != -1] {$contents .= $str;}}
else {
$lastPos = 0;
for [$i = 0; $i < $number_of_occurences; $i++]{
$pos = strpos[$str,"\n", $lastPos];
$occurence++;
if [$occurence == $line] {
$startPos = $pos;
if [$i == $number_of_occurences - 1] {$contents = substr[$str, $startPos + 1];}
} elseif [$occurence == $line + 1] {
if [$i == 0] {$contents .= substr[$str, 0, $pos];} else {$contents = substr[$str, $startPos, $pos - $startPos];}
$occurence = 0;
break;
}
$lastPos = $pos + 1;
}
}
}
@fclose[$fp];
return $contents;
}
?>
I conducted many tests [I believe to be fair], and here's one of the results:
0
I conducted many tests [I believe to be fair], and here's one of the results:
1
I conducted many tests [I believe to be fair], and here's one of the results:
2
I conducted many tests [I believe to be fair], and here's one of the results:
3