This is interesting. I took the code above and changed the fgets() statement to this..
while( $lines[] = fgets( $fp, 1024 ) ) {
#nothing
}
And added this to the fread() statement
reset( $lines );
while( list( $name ) = each( $lines ) ){
$lines[$name] .= "\n";
}
That takes the advantage off of the fread over file. After this, I took and ran the same benchmark three times in a table to see how they stacked up repeatedly. Here's what I got..
1st
Max time: 0.482866
Min time: 0.034758
Average of fread over 20 times: 0.1334147
2nd
Max time: 0.212973
Min time: 0.038108
Average of fread over 20 times: 0.07108205
3rd
Max time: 0.276189
Min time: 0.034615
Average of fread over 20 times: 0.09126225
1st
Max time: 0.292695
Min time: 0.006059
Average of file() over 20 times: 0.04336025
2nd
Max time: 0.113599
Min time: 0.006252
Average of file() over 20 times: 0.01594175
3rd
Max time: 0.053411
Min time: 0.006066
Average of file() over 20 times: 0.011429
1st
Max time: 0.582056
Min time: 0.021118
Average of fgets() over 20 times: 0.20118685
2nd
Max time: 0.347907
Min time: 0.022264
Average of fgets() over 20 times: 0.14828945
3rd
Max time: 0.679353
Min time: 0.021692
Average of fgets() over 20 times: 0.2773081
I was running a 325 line 15k file through it, and file (to my amazement) does the best. file() is the only one that had a .00 as the low, and it had the lowest peaks.
One note on the code you posted, you need to add "$max=0;" on the line after "$min=100;" to get an accurate max reading..
Travis