PHP中fwrite與file_put_contents效能測試代碼_php技巧

來源:互聯網
上載者:User

function microtimeFloat() {
    list($usec,$sec) = explode(" ", microtime());
    return((float)$usec + (float)$sec);
}

1.測試file_put_contents

複製代碼 代碼如下:

<?php
$userCount = 1000;
$itemCount = 1000;
$file = 'ratings.txt';
file_exists($file) &&unlink($file);

$timeStart = microtimeFloat();
for ($i = 0; $i < $userCount; $i++) {
    $uid =random(32);
    for ($j = 0;$j < $itemCount; $j++) {
       $itemId = mt_rand(1, 300000);
       $rating = $j == 0 ? 1 : mt_rand(1, 100) / 100;
       $line = sprintf("%s,%d,%s\n", $uid, $itemId, $rating);
       file_put_contents($file, $line, FILE_APPEND);
    }
}
$timeEnd = microtimeFloat();
echo sprintf("Spend time: |%s| second(s)\n", $timeEnd -$timeStart);
?>

測試結果:
測試過程中出現了開啟檔案的錯誤,而且程式執行完成以後寫入的資料不完整,只有999997行,漏了3行。最重要的一點是時間花了307秒多,而用fwrite只花了10秒多的時間,差距還是不小的。

D:\myphp\research>php test2.php
PHP Warning: file_put_contents(ratings.txt): failed to open stream:Permission
denied in D:\myphp\research\test2.php on line 79

Warning: file_put_contents(ratings.txt): failed to open stream:Permission denie
d in D:\myphp\research\test2.php on line 79
Spend time: |307.0586669445|second(s)

...
999994:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,167670,0.15
999995:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,234223,0.13
999996:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,84947,0.79
999997:98xDtLjaeD8MG9ywifegZvRRQzVBZbbw,6489,0.38

2.測試fwrite

複製代碼 代碼如下:

<?php
$userCount = 1000;
$itemCount = 1000;
$file = 'ratings.txt';
file_exists($file) &&unlink($file);

$fp = @fopen($file, 'ab');
if (!$fp) die("Open $file failed");

$timeStart = microtimeFloat();
for ($i = 0; $i < $userCount; $i++) {
    $uid =random(32);
    for ($j = 0;$j < $itemCount; $j++) {
       $itemId = mt_rand(1, 300000);
       $rating = $j == 0 ? 1 : mt_rand(1, 100) / 100;
       $line = sprintf("%s,%d,%s\n", $uid, $itemId, $rating);
       fwrite($fp, $line);
       $k++;
    }
}
if ($fp) @fclose($fp);
$timeEnd = microtimeFloat();
echo sprintf("Spend time: |%s| second(s)\n", $timeEnd -$timeStart);
?>

測試結果:
寫一百萬行記錄,10秒左右寫完,對於PHP來說,速度算不錯了。這是在我的個人電腦上面測試的,如果在生產機上測試,可能速度還要快一些。
D:\myphp\research>php test2.php
Spend time: |10.764221191406|second(s)

用fwrite寫入的資料是完整的
999997,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,246982,0.03
999998,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,240160,0.39
999999,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,46296,0.61
1000000,QOvcZYFJFlFHjiGyGxAc615kOXdX3Yii,26211,0.14

3.總結
如果要往檔案裡面寫入大量的資料,則推薦用fwrite,不要用file_put_contents。在高並發的請求中也建議用fwrite。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.