Reputation: 1181
I was checking the difference in execution time of these 4 strings with same variable:
$name= "rahul";
echo "My name is $name"
echo "My name is ".$name;
echo "My name is {$name}";
echo "My name is '{$name}'";
But every i tried it i got only 3 or 4 random answers ie. 0 OR 0.00099992752075195 OR 0.0010001659393311
My code is :
<?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";
?>
I was calculating it through cmd and my output was:
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>
why am i getting same output again and again with different statements.
Upvotes: 0
Views: 295
Reputation:
The execution time of your script is really negligible. For clear results, you should use a loop statement with 100.000 iterations for example per statement.
Upvotes: 0
Reputation: 19915
These statements take less than I millisecond to execute. Since your precision is one millisecond you cannot get a meaningful result.
If you want to benchmark this, you must iterate a large number of times, like 10 000 times.
Also your benchmark will not allow you to measure the difference between the string representations, because the output of the string through echo takes more time than the evaluation of the expression.
Upvotes: 1
Reputation: 1560
Differences are to low. Try to repeat every action at least 10 000 times, then you should see any comparable results. You dont need to use echo, assigning to another var will work the same.
Why your results are so random? Because result times are so small that any other server processes may affect your results. Try this version:
<?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
?>
In addition, many other developers proved that the best solution is to use
echo 'My name is ' . $name;
style but differencies between others are not as important :)
Upvotes: 2