|
stringfile_get_contents(string$filename[,bool$use_include_path=false[,resource$context[,int$offset=-1[,int$maxlen]]]])
|
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
下面将老外用1MB文件来进行测试的结果也贴一下,有兴趣的朋友补充测试.
Function | Sample Usage | Time (s) | Memory (b) |
file_get_contents |
|
echofile_get_contents($filename);
|
|
0.00564 |
1067856 |
fpassthru |
|
0.00184 |
20032 |
fgets |
|
$fp=fopen($filename,"rb");
while(!feof($fp))
{
echofgets($fp);
}
|
|
0.07190 |
30768 |
file |
|
echojoin("",file($filename));
|
|
0.06464 |
2185624 |
require_once |
|
0.08065 |
2067696 |
include |
|
0.08202 |
2067696 |
readfile |
|
0.00191 |
19208 |
接着是32KB的小文件处理上
Function | Time (s) | Memory (b) |
|
32Kb File |
1Mb File |
32Kb File |
1Mb File |
file_get_contents |
0.00152 |
0.00564 |
52480 |
1067856 |
fpassthru |
0.00117 |
0.00184 |
20016 |
20032 |
fgets |
0.00195 |
0.07190 |
30760 |
30768 |
file |
0.00157 |
0.06464 |
87344 |
2185624 |
require_once |
0.00225 |
0.08065 |
67992 |
2067696 |
include |
0.00222 |
0.08202 |
67928 |
2067624 |
readfile |
0.00117 |
0.00191 |
19192 |
19208 |
用上面的结果的话,readfile 和fpassthru 是我们最佳的选择.