正規表現を使ったほうが早い
$data = file_get_contents("file.txt");
preg_match_all("/([0-9]{10}) ([0-9]{3}\.[0-9]{2})/",$data,$Matches);
//Use below if you want an associative array with the first 10 numbers
//being the keys and the second numbers being the values
$myData = array_combine($Matches[1],$Matches[2]);
([0-9]{10})
0 ~ 9 の最初の 10 個の数字に一致します。
([0-9]{3}\.[0-9]{2})
0 ~ 9 の 3 つの数字、ピリオド、さらに 0 ~ 9 の 2 つの数字の次の数字のセットに一致します。
$Matches は
Array
(
[0] => Array
(
[0] => 1303100643 115.83
[1] => 1303100644 115.94
[2] => 1303100645 115.80
[3] => 1303100646 115.99
[4] => 1303100647 115.74
[5] => 1303100648 115.11
)
[1] => Array
(
[0] => 1303100643
[1] => 1303100644
[2] => 1303100645
[3] => 1303100646
[4] => 1303100647
[5] => 1303100648
)
[2] => Array
(
[0] => 115.83
[1] => 115.94
[2] => 115.80
[3] => 115.99
[4] => 115.74
[5] => 115.11
)
)
コード対コード:
ジェイソン・マクレアリー
$time1=microtime();
$mydata = array();
$file_handle = fopen("data.txt","r");
while (!feof($file_handle) ) {
set_time_limit(0);
$line_of_text = fgets($file_handle, 1024);
$reading=explode(" ", $line_of_text);
$mydata[] = $reading;
}
fclose($file_handle);
$time2 =microtime();
行ごとの読み取りと爆発の使用
1374728889 0.20137600 :: 1374728889 0.20508800
0.20508800
0.20137600
----------
0.00371200
私の
$time1=microtime();
$data = file_get_contents("data.txt");
preg_match_all("/([0-9]{10}) ([0-9]{3}\.[0-9]{2})/",$data,$Matches);
$myData = array_combine($Matches[1],$Matches[2]);
$time2=microtime();
echo $time1." :: ".$time2;
fgc と正規表現の使用
1374728889 0.20510100 :: 1374728889 0.20709000
0.20709000
0.20510100
----------
0.00198900