不精确和随机的代码执行时间

时间:2013-08-17 11:15:23

标签: php microtime

我正在使用相同的变量检查这4个字符串的执行时间差异:

$name= "rahul";
echo "My name is $name"  
echo "My name is ".$name;  
echo "My name is {$name}"; 
echo "My name is '{$name}'";

但每次我尝试过它我只得到3或4个随机答案,即。 0 OR 0.00099992752075195 OR 0.0010001659393311

我的代码是:

<?php
function microtime_float()
 {
     list($usec, $sec) = explode(" ", microtime());
     return ((float)$usec + (float)$sec);
 }

 $name = 'rahul' ;

 $time_start = microtime_float();
 echo "My name is $name";  // First style
 $time_end = microtime_float();
 $time = $time_end - $time_start;

 echo "  // $time seconds\n   ";



 $time_start2 = microtime_float();
 echo "My name is ".$name;  // Second style
 $time_end2 = microtime_float();
 $time2 = $time_end2 - $time_start2;

 echo " // $time2 seconds\n   ";



 $time_start3 = microtime_float();
 echo "My name is {$name}";  // Third style
 $time_end3 = microtime_float();
 $time3 = $time_end3 - $time_start3;

 echo " // $time3 seconds\n";



 $time_start4 = microtime_float();
 echo "My name is '{$name}'";  // Fourth style
 $time_end4 = microtime_float();
 $time4 = $time_end4 - $time_start4;

 echo " // $time4 seconds\n";

 ?>

我是通过cmd计算的,我的输出是:

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0.00099992752075195 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

   My name is rahul // 0.0010001659393311 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

   My name is rahul // 0.00099992752075195 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

   My name is rahul // 0.00099992752075195 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0.00099992752075195 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0.00099992752075195 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0.00099992752075195 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0.0010001659393311 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0.0010001659393311 seconds

   My name is rahul // 0.00099992752075195 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0.00099992752075195 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

My name is 'rahul' // 0.0010001659393311 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0 seconds

   My name is rahul // 0.0010001659393311 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

   My name is rahul // 0 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>php q.php

My name is rahul  // 0 seconds

   My name is rahul // 0.00099992752075195 seconds

   My name is rahul // 0.00099992752075195 seconds

My name is 'rahul' // 0 seconds

C:\wamp\www\my_jquery>

为什么我会一次又一次地使用不同的声明获得相同的输出。

3 个答案:

答案 0 :(得分:2)

差异很小。尝试重复每个动作至少10000次,然后你应该看到任何可比的结果。你不需要使用echo,分配给另一个var将会起作用。

为什么你的结果如此随机?由于结果时间非常短,因此任何其他服务器进程都可能会影响您的结果。试试这个版本:

<?php
function microtime_float()
 {
     list($usec, $sec) = explode(" ", microtime());
     return ((float)$usec + (float)$sec);
 }

 $times = 10000;

 $name = 'rahul' ;

 $time_start = microtime_float();

 // Make loop
 for($i = 0; $i < $times; $i++) {
     $testvar = "My name is $name";  // First style
 }

 $time_end = microtime_float();
 $time = $time_end - $time_start;
 echo "  // $time seconds\n   ";

 //and same for other styles

 ?>

此外,许多其他开发人员证明最佳解决方案是使用

echo 'My name is ' . $name;
风格但其他人之间的差异并不那么重要:)

答案 1 :(得分:2)

这些语句执行时间不到1毫秒。由于精度为1毫秒,因此无法获得有意义的结果。

如果要对此进行基准测试,则必须迭代很多次,例如10000次。

此外,您的基准测试不允许您测量字符串表示之间的差异,因为字符串通过echo的输出比表达式的评估花费更多的时间。

答案 2 :(得分:0)

脚本的执行时间实际上可以忽略不计。为了获得清晰的结果,您应该使用带有100.000次迭代的循环语句,例如每个语句。