使用正则表达式会更快
$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})
匹配前 10 个数字 0-9,
([0-9]{3}\.[0-9]{2})
匹配下一组具有 3 个数字 0-9 的数字,然后是一个句点,然后是 2 个数字 0-9
$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();
逐行阅读并使用explode
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