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 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了