0

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.

DCoder
  • 12,962
  • 4
  • 40
  • 62
Rahul
  • 1,181
  • 1
  • 11
  • 20
  • you should link to the output saved in some other place, it makes the question too long.. also check the man page for microtime, the function has an optional parameter that you can use (http://php.net/microtime) – mishu Aug 17 '13 at 11:20
  • 1
    the time taken is so extremely small for a single echo, you need to put in a loop and try it 10k times and time that –  Aug 17 '13 at 11:20
  • 1
    You are measuring not only the different echos, but also the capturing of the time, and processing it. It also is not necessary to eliminate that space, call `microtime(true)` to get a ready-made measurement value. – Sven Aug 17 '13 at 11:21
  • 1
    A sample size of one is not a valid statistic, it's an anecdote. To get a meaningful answer, you need to run the fragment of code you're measuring *multiple* times and get *an average*. That said, **stop wasting time on something as trivial as this** and optimize your algorithms instead. A 50% speedup in a function that is responsible for 0.1‰ of the entire program is not worth the time it takes to test it, let alone implement it. (Edit: just for reference, there's one more style: `echo "My name is ", $rahul;`) – DCoder Aug 17 '13 at 11:23

3 Answers3

2

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 :)

Tomasz Banasiak
  • 1,540
  • 2
  • 13
  • 19
2

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.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
0

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.