对一个大文件进行逐行遍历,如下方法性能较高的是?

A:写一个实现了IteratorAggregate 接口的类,通过该类使用foreach遍历。
B:使用file_get_contents 将文件内容一次性载入内存,然后逐行遍历。
C:通过exec函数,调用shell 工具遍历
D:使用别人写的类库

正确答案:A

答案分析:使用 IteratorAggregate 可将文件打开后通过移动指针的方式逐行遍历,不受文件大小影响。使用 file_get_contents 处理大文件很容易导致PHP内存溢出;调用exec 会产生额外的进程,影响性能;其他人写的类库质量不一定高。

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_55179.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?