function microtimeFloat() {
list($usec
return((float)$usec + (float)$sec);
}
<?php
$userCount =
$itemCount =
$file =
file_exists($file) &&unlink($file);
$timeStart = microtimeFloat();
for ($i =
$uid =random(
for ($j =
$itemId = mt_rand(
$rating = $j ==
$line = sprintf("%s
file_put_contents($file
}
}
$timeEnd = microtimeFloat();
echo sprintf("Spend time: |%s| second(s)\n"
?>
測試結果:
測試過程中出現了打開文件的錯誤
D:\myphp\research>php test
PHP Warning: file_put_contents(ratings
denied in D:\myphp\research\test
Warning: file_put_contents(ratings
d in D:\myphp\research\test
Spend time: |
<?php
$userCount =
$itemCount =
$file =
file_exists($file) &&unlink($file);
$fp = @fopen($file
if (!$fp) die("Open $file failed");
$timeStart = microtimeFloat();
for ($i =
$uid =random(
for ($j =
$itemId = mt_rand(
$rating = $j ==
$line = sprintf("%s
fwrite($fp
$k++;
}
}
if ($fp) @fclose($fp);
$timeEnd = microtimeFloat();
echo sprintf("Spend time: |%s| second(s)\n"
?>
測試結果:
寫一百萬行記錄
D:\myphp\research>php test
Spend time: |
用fwrite寫入的數據是完整的
如果要往文件裡面寫入大量的數據
From:http://tw.wingwit.com/Article/program/PHP/201311/21319.html