0

Trying to test how long mongo takes to retrieve each document.

However, system() prints the result to screen and I think that may slow down the process.

How could I supress it from printing to screen?

<?php
$m = new MongoClient();
$db = $m->selectDB("test");
$collection = new MongoCollection($db, "fakestat");
for($i=0;$i<1000;$i++){
        $index[]=rand(1,432050);
}
$fp = fopen('logTime.txt','w');
fwrite($fp, "startTimeSec\t startTimeNanoSec\t endTimeSec\t endTimeNanoSec\n");

foreach($index as $val) {
        $startTime = system("date +%s'\t'%N");
        $results[] = $collection->findOne(array("capture"=>"$val"));
        $endTime = system("date +%s'\t'%N");
        fwrite($fp, sprintf("%s\t %s\n", $startTime, $endTime));
}
fclose($fp);
$m->close();
?>

Update: Using exec() and it seems to be work abeit still rather slow.

4

1 回答 1

1

两种选择:

  1. 使用shell_exec而不是system

    $startTime = shell_exec("date +%s'\t'%N");
    
  2. 请改用微时间

    microtime — 返回当前的 Unix 时间戳,以微秒为单位

    $startTime = microtime();
    
于 2013-02-18T02:34:08.247 回答