php 什么更快?file_put_contents(); fopen(); fwrite(); fclose();?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6415958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:19:19  来源:igfitidea点击:

What's faster? file_put_contents(); fopen(); fwrite(); fclose();?

phpfile-io

提问by genesis

What is better? I have my own MySQL log so it has about 90MB, but I'm not sure which one should I use. It opens file EVERYTIME there is query to execute.

什么是更好的?我有自己的 MySQL 日志,所以它有大约 90MB,但我不确定应该使用哪一个。它每次有要执行的查询时都会打开文件。

What's faster?

什么更快?

回答by hwrdprkns

According to this article, the fwrite()is a smidgen faster. (Link to Wayback Machine, because site no longer exists.)

根据这篇文章,这fwrite()是一个更快的速度。(链接到 Wayback Machine,因为站点不再存在。)

My guess is that file_put_contents()is just a wrapper around those three methods anyways, so you would lose the overhead.

我的猜测是file_put_contents()无论如何这只是这三种方法的包装器,所以你会失去开销。

EDIT: This sitehas the same information.

编辑该站点具有相同的信息。

回答by shaunhusain

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

该函数等同于依次调用 fopen()、fwrite() 和 fclose() 将数据写入文件。

Check the docs: http://php.net/manual/en/function.file-put-contents.php

检查文档:http: //php.net/manual/en/function.file-put-contents.php

Shaun

肖恩

回答by cwallenpoole

Depends on your use-case. I've opened files which were over .5 GB, and I certainly didn't want to use file() or file_get_contents(); And file_put_contents couldn't work because I needed to read the file too.

取决于您的用例。我打开了超过 0.5 GB 的文件,我当然不想使用 file() 或 file_get_contents(); 而且 file_put_contents 无法工作,因为我也需要读取文件。

If you're really just interested in appending a file without reading, it doesn't terribly matter; if you're trying to read a whole file into memory (or write a whole file from memory), it similarly does not really matter -- the speed gain, as near as I've seen, is round-off error.

如果您真的只是对附加文件而不阅读感兴趣,那也没什么关系;如果您试图将整个文件读入内存(或从内存中写入整个文件),同样也无关紧要——我所看到的速度增益是舍入误差。

BUT, if you're expecting that these files will ever grow to gargantuan beasts, or if you only need a small subset of the number of lines of a given file, I cannot suggest use of fopen (or the SplFileObject, which is AWESOME) strongly enough -- it is really easy to read from the middle of a file with these.

但是,如果您期望这些文件会变成巨大的野兽,或者如果您只需要给定文件行数的一小部分,我不能SplFileObject强烈建议使用 fopen(或 .足够了——用这些从文件的中间读取真的很容易。



Since you're just logging, on the other hand, I, personally, find it clearer and more concise to simply use file_put_contents with the append flag. It lets everyone know what's going on without having to look twice.

另一方面,由于您只是在进行日志记录,因此我个人认为将 file_put_contents 与 append 标志一起使用更清晰、更简洁。它让每个人都知道发生了什么,而不必看两次。

回答by flykobe

  1. If there is only one write-operation for one file in a request, file_put_contents is better than fwrite, because it has wrappered open, write and close functions, and as fast as fwrite.
  2. If there are multi-write for one file in a request, we need open file first, then write multi-times, so fwrite is faster. But, if it is a very busy system, which has a lot of "open files"(ulimit -a | grep "open files"), file_put_contents will be a more reasonable choose, because it keep the file handler shorter than fwrite.
  1. 如果一个请求中只有一个文件的写操作,file_put_contents 比 fwrite 好,因为它封装了 open、write 和 close 函数,和 fwrite 一样快。
  2. 如果一个请求中一个文件有多次写入,我们需要先打开文件,然后再写入多次,所以fwrite更快。但是,如果它是一个非常繁忙的系统,它有很多“打开文件”(ulimit -a | grep “打开文件”),file_put_contents 将是一个更合理的选择,因为它使文件处理程序比 fwrite 更短。

回答by Marc B

As per usual, have you benchmarked it? file_put_contents is essentially just a wrapper around the 3 f*()calls anyways, so at most you're losing out by doing one extra function call, but gaining somewhat by having less front-end code to parse.

按照惯例,您是否对其进行了基准测试?f*()无论如何,file_put_contents 本质上只是 3 个调用的包装器,所以最多你会因为执行一个额外的函数调用而失败,但是通过减少前端代码来解析而有所收获。