duanji9481 2015-12-11 07:35
浏览 186
已采纳

使用fputcsv有什么优点/缺点?

Below code is from the documentation of fputcsv:

<?php
$time_start = microtime(true);
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "time is: $time";
?> 

time is: 0.0037028789520264
time is: 0.0036959648132324
time is: 0.0037329196929932

why not just use something like the below code? they seemed like they have the same speed? In this way, you control what kind of new line you need to use like or

<?php
$time_start = microtime(true);
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fwrite($fp, implode(',', $fields) . "
");
}

fclose($fp);

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "time is: $time";
?> 

time is: 0.0037031173706055
time is: 0.0037119388580322
time is: 0.0036849975585938

DISCLAIMER: I dont know if im using too small number of fields to benchmark

  • 写回答

1条回答 默认 最新

  • dongqing4070 2015-12-11 07:44
    关注

    fputcsv is a helper function. Yes you can do it manualy. Under the hood there would probably be some simmelair funtionality. But it is eassier to implement this way and improves readability. You can just throw in the array and don't have to worry about escaping and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭