PHP操作文件函数比较(转)

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文件来进行测试的结果也贴一下,有兴趣的朋友补充测试.

FunctionSample UsageTime (s)Memory (b)
file_get_contents 0.00564 1067856
fpassthru 0.00184 20032
fgets 0.07190 30768
file 0.06464 2185624
require_once 0.08065 2067696
include 0.08202 2067696
readfile 0.00191 19208

接着是32KB的小文件处理上

FunctionTime (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 是我们最佳的选择.

原文地址:https://www.cnblogs.com/xingmeng/p/3110456.html