使用正則表達式會更快
$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
$比賽將是
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
)
)
代碼VS代碼:
JasonMcCreary
$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
你是一個救生員:D – Waqas