file_put_contents and fputs
来源:互联网 发布:淘宝开店认证在哪里弄? 编辑:程序博客网 时间:2024/05/21 09:46
test code
<?phpfile_put_contents('/tmp/test.log',"");$workers = 100;for ($i = 0; $i < $workers; ++$i) { $pid = pcntl_fork(); if (!$pid) { process($i); exit($i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } echo "ok";function process($i){ $n = 10000; while ($n > 0) { file_put_contents('/tmp/test.log', "{$i} {$n} \n", FILE_APPEND); $n--; }}function _process($i){ $fp = fopen('/tmp/test.log', 'a'); $n = 10000; while ($n > 0) { fputs($fp, "{$i} {$n} \n"); $n--; } fclose($fp);}
run
time php file_test.php
file_put_contests
okreal 0m21.648s
user 0m6.740s
sys 0m11.333s
ok
real 0m4.599s
user 0m1.572s
sys 0m3.128s
由此可以看出fput比file_put_contents要快4倍左右
在web app中经常要记录一些日志,一次请求中可能要几率好几条日志,可见用puts是比较优的方法
另外
我发现很多人喜欢在最加文件内容时加锁 flock,其实没有必要,fwrite是原子性的
Note:
If handle
was fopen()ed in append mode, fwrite()s are atomic (unless the size of string
exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to flock() a resource before calling fwrite(); all of the data will be written without interruption.
0 0
- file_put_contents and fputs
- file_put_contents
- file_put_contents
- fputs
- fputs
- file_put_contents()函数
- fputc & fputs
- fputs请求
- fets & fputs
- fgets fputs
- fgets,fputs
- fputs version
- php 生成配置文件 file_put_contents
- PHP file_put_contents() 函数
- PHP file_put_contents() 函数
- exec() file_get_contents() file_put_contents() 示例
- file_put_contents写入文件路径
- PHP file_put_contents() 函数
- 小白---VSS 的记住密码
- Docker学习小结
- 程序异常处理
- 2015 Multi-University Training Contest 7
- 畅通工程再续
- file_put_contents and fputs
- 网络中的长连接和短链接
- 关于show tables命令的一场探险
- 1053. Path of Equal Weight (30)
- CC_CALLBACK原理及应用
- $.ajax 提交数据
- Android之Inflate()方法用途
- 不带缓冲IO和带缓冲IO
- 26-网络编程-08-网络编程(UDP协议-接收端)