1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

phpでcsvファイルを読込み配列に格納するまでの速度

Last updated at Posted at 2019-12-11

file()を使用する場合とfgetcsv()を使用する場合で速度を比較した。
レコードは数値のみの想定。今回試したレコード数は10240件。

csvファイル
11.11,11.11
22.22,22.22
…

file()を使用した場合

かかった時間:0.0099518299102783

file()
$time_start = microtime(true);

$data = array();
$cnt = 0;
$lines = file('timetest.csv');
foreach($lines as $line){
    $arr = explode(',' , $line);
    $data[$cnt]['hoge'] = $arr[0];
    $data[$cnt]['fuga'] = $arr[1];
    $cnt++;
}
$time = microtime(true) - $time_start;
echo "{$time} 秒";

fgetcsv()を使用した場合

かかった時間:0.031763076782227

fgetcsv()
<?php
$time_start = microtime(true);

$data = array();
$cnt = 0;
$fp = fopen('timetest.csv', 'r');
while(($line = fgetcsv($fp)) !== FALSE){
    $data[$cnt]['hoge'] = $line[0];
    $data[$cnt]['fuga'] = $line[1];
    $cnt++;
}
fclose($fp);

$time = microtime(true) - $time_start;
echo "{$time} 秒";

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?