Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Quick and Dirty "Which of these is better/faster/proper?"

By: a guest on Nov 3rd, 2013  |  syntax: PHP  |  size: 1.17 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4.  * Test question from reddit whether than closing PHP tag and echoing is faster
  5.  * than echo function.
  6.  */
  7.  
  8. /**
  9.  * The total number of times each test will run.
  10.  *
  11.  * @var int
  12.  */
  13. $iterations =  50000;
  14.  
  15. /**
  16.  * Stores the total number of microseconds of runtime.
  17.  */
  18. $runtime = 0;
  19.  
  20. //////////// FIRST TEST //////////////////
  21.  
  22. $a = 'a';
  23. $b = 'a';
  24. ob_start();
  25. for( $i = 0; $i < $iterations; $i++) {
  26.  
  27.        
  28.         $start = microtime();
  29.        
  30.         if($a == $b) : ?><li>My gran can beat you at <?php echo $b; ?> no problem.</li><?php endif;
  31.  
  32.         $end = microtime();
  33.  
  34.         $runtime += ($end - $start);
  35. }
  36. ob_end_clean();
  37.  
  38. $avgRuntime = $runtime / $iterations;
  39.  
  40. printf( "Test 1 ran %s times. Average runtime was %s microseconds\n", $iterations, $avgRuntime );
  41.  
  42. $runtime = 0;
  43.  
  44. $a = 'a';
  45. $b = 'a';
  46. ob_start();
  47. for( $i = 0; $i < $iterations; $i++) {
  48.  
  49.         $start = microtime();
  50.  
  51.         if($a == $b) echo '<li>My gran can beat you at '. $b .' No problem.</li>';
  52.  
  53.         $end = microtime();
  54.  
  55.         $runtime += ($end - $start);
  56. }
  57. ob_end_clean();
  58. $avgRuntime = $runtime / $iterations;
  59.  
  60. printf( "Test 2 ran %s times. Average runtime was %s microseconds\n", $iterations, $avgRuntime );
clone this paste RAW Paste Data