测量SQL-Query的时间,由PDO发送

时间:2014-01-20 21:33:53

标签: php mysql pdo timing

我尝试测量PDO元素发送查询和检索答案所需的时间。

$db = new PDO('mysql:dbname=dbname;host=dbhost;charset=utf8', "username", "password");

$timing = -microtime(true);

    $query = $db->prepare('SELECT * FROM table WHERE ID = 1');
    $query->execute();
    $result = $query->fetch();

var_dump($timing + microtime(true));

结果应该是大约0.0012秒,至少这是phpmyadmin显示的持续时间。但结果总是如此:

float(0)

是否存在某种舍入问题,我不知道?

  • 操作系统:WindowsXP
  • PHP:5.4.16
  • MySQL:5.1.51
  • 服务器:Apache 2.4.4

1 个答案:

答案 0 :(得分:2)

没有比手册更好的代码

$time_start = microtime(true);

//your code

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "it took $time seconds\n";