zero-perfoliate
zero-perfoliate

Author Topic: PHP Efficiency Tests  (Read 929 times)

hotnoob

  • Guest
PHP Efficiency Tests
« on: March 24, 2010, 11:37:32 AM »
K, so i was bored as wondering a few things.

So, i made some scripts to help test my guesses.

Single Vs Double Quote

For the first one i predicted that static text added by a variable would be the most efficient.
My initial hypothesis was correct.
The test concluded that

echo 'TEST Run'.$runNo;

was the most efficient method.
My test notes are here; if you want to see the alternatives i used just go there:
http://phpbar.isgreat.org/viewtopic.php?f=2&t=56

Variables vs Arrays

For the second test, i predicted that associated arrays would be a suitable replacement for variables such as $var; Unfortunately i was wrong.

Surprisingly enough, the result was that normal variables are the most efficient.

Here are my notes and results:
http://phpbar.isgreat.org/viewtopic.php?f=2&t=59

--
Well i solved my curiosity with this :D
hope i helped you guys :D

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #1 on: March 24, 2010, 03:21:57 PM »
What were the tests you ran to confirm this?

for test one how about: echo 'TEST Run$runNo'; ?

for test two, how about: $var = array(1,2,3,4,5); ?
how about: $var = array(1=>1, 2=>2, 3=>3, 4=>4, 5=>5); ?

o____o //hopeful.

hotnoob

  • Guest
Re: PHP Efficiency Tests
« Reply #2 on: March 24, 2010, 10:01:47 PM »
um, you do realize that
single quotes is for static information, double is for dynamic information.

that is why i did not test 'TEST Run$runNo'

The purpose of the second test was to rule out the usage of arrays instead of variables.

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #3 on: March 25, 2010, 04:12:48 AM »
Isee. But hat were the tests you ran? did you just store the variables and see how long that took or what?

hotnoob

  • Guest
Re: PHP Efficiency Tests
« Reply #4 on: March 25, 2010, 08:39:41 AM »
Yes, i recorded the time it took to do it... and no i didn't use a stopwatch :P

This is how you measure the time:
http://phpbar.isgreat.org/viewtopic.php?f=2&t=43

here is the script that i used to measure the efficiency of the quotes. i use a similar one for the second test

Code: [Select]
<?php
      $runNo 1;
      $runTime 0;
      $runMax 100;
      $result1No 0;
      $result2No 0;
      $result3No 0;
      while($runTime $runMax)
      {
      $runTime++;
      $max 50;
      $count 0;
                      
$time microtime();
                      
$time explode(' '$time);
                      
$time $time[1] + $time[0];
                      
$start $time;
      while($count $max)
      {
echo "TEST Run".$runNo;
$count++;
      }
      $time microtime();
                      
$time explode(' '$time);
                      
$time $time[1] + $time[0];
                      
$finish $time;
                      
$total_time round(($finish $start), 6);
                      
$result1 .= '<p style="font-size: 12px; color: grey;">Generated in '.$total_time.' seconds.</p>'."\n";
      $result1No += $total_time;
      
      $count 0;
      $time microtime();
                      
$time explode(' '$time);
                      
$time $time[1] + $time[0];
                      
$start $time;
      while($count $max)
      {
echo 'TEST Run'.$runNo;
$count++;
      }
      $time microtime();
                      
$time explode(' '$time);
                      
$time $time[1] + $time[0];
                      
$finish $time;
                      
$total_time round(($finish $start), 6);
                      
$result2 .= '<p style="font-size: 12px; color: grey;">Generated in '.$total_time.' seconds.</p>'."\n";
      $result2No += $total_time;
      
      $count 0;
      $time microtime();
                      
$time explode(' '$time);
                      
$time $time[1] + $time[0];
                      
$start $time;
      while($count $max)
      {
echo "TEST Run $runNo";
$count++;
      }
      $time microtime();
                      
$time explode(' '$time);
                      
$time $time[1] + $time[0];
                      
$finish $time;
                      
$total_time round(($finish $start), 6);
                      
$result3 .= '<p style="font-size: 12px; color: grey;">Generated in '.$total_time.' seconds.</p>'."\n";
      $result3No += $total_time;
      }
echo "<div style='background-color:#EEEEEE;'>RESULT 1 $result1</div>";
echo "<div style='background-color:#EEEEEE;'>RESULT 2 $result2</div>";
echo "<div style='background-color:#EEEEEE;'>RESULT 3 $result3</div>";

echo "Tested $runMax Time"
echo "<div style='background-color:#EEEEEE;'>RESULT 1 Total $result1No</div>";
echo "<div style='background-color:#EEEEEE;'>RESULT 2 Total $result2No</div>";
echo "<div style='background-color:#EEEEEE;'>RESULT 3 Total $result3No</div>";


?>


Feel free to try do your own efficency tests.
Note* for what it's worth, you should only really worry about this if you have limited resources and a high amount of traffic.
like... 50k+ visitors a day, or if you have a slow server(slow as in proccessing; if your having trouble with the data transfer, you should look into sending compressed data and using javascript to decompress and run).


Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #5 on: March 25, 2010, 05:00:39 PM »
i know you would loose precision, but why didn't you truncate or round the microtime difference?

hotnoob

  • Guest
Re: PHP Efficiency Tests
« Reply #6 on: March 25, 2010, 08:43:00 PM »
i did round.

And for that very exact reasons. When it comes to Measure execution times for small bits of code, rounding it to 4 digits would return 0, or something crazy like that.

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #7 on: March 26, 2010, 03:51:32 PM »
oh, right... hmmm...

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #8 on: March 30, 2010, 10:40:08 AM »
Got a test for the vars, V. Arrays. As you said, arrays are not as fast as non-arrays. Which makes it basically, if you don't need an array, don't use it. On the other hand, if you need to have an array, non-arrays are not that much faster to stop using arrays completely.
I agree with your tests.

Code: [Select]
<?php

$i 0;
$b_time microtime();
while ($i 10000) {
$a 1;
$b 2;
$c 'todd';
$d "penelope";
$e $a.$b.$c.$d.$e;
$a='';
$b='';
$c='';
$d='';
$e='';
$i++;
}
$e_time microtime();
$timetaken round($e_time $b_time4); 
echo "<br />Test one took: $timetaken seconds.";

$i 0;
$b_time microtime();
while ($i 10000) {
$arr['a'] = 1;
$arr['b'] = 2;
$arr['c'] = 'todd';
$arr['d'] = "penelope";
$arr['e'] = $a.$b.$c.$d.$e;
$arr['a']='';
$arr['b']='';
$arr['c']='';
$arr['d']='';
$arr['e']='';
$i++;
}
$e_time microtime();
$timetaken round($e_time $b_time4); 
echo "<br />Test one took: $timetaken seconds.";


//results show non-arrays in 10000 passes are faster by 0.005x
//not slow enough to suggest non-arrays should be used in every situation, only when arrays are not required
?>


hotnoob

  • Guest
Re: PHP Efficiency Tests
« Reply #9 on: March 30, 2010, 12:47:49 PM »
Yeah, with the arrays i really just wanted to determine if i should go
$config['configStuff'];
or
$cfig_configStuff;

---
for some odd reason, i get the feeling that you are my computers teacher...
cause my teacher's first name is Rose.
:P

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #10 on: March 30, 2010, 02:19:09 PM »
I am neither a Rose nor the gender Rose normal presents. FYI.

So, whats up with the underlines in php anyway? I never really understood them.

hotnoob

  • Guest
Re: PHP Efficiency Tests
« Reply #11 on: March 30, 2010, 05:11:25 PM »
It's really just a way to keep track of variables... other than that it means nothing...

$_
^^ means it's usually a built in variable

and everything else is just a way to keep track.

like some time i'll use $u_ for user variables like $u_id or $u_name.

It helps when i have variables with the same name but from different sources.

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Efficiency Tests
« Reply #12 on: March 30, 2010, 06:53:25 PM »
oh, very nice. I guess I was just used to java's way of doing it. with uppercase every first letter of every word after the first.

hotnoob

  • Guest
Re: PHP Efficiency Tests
« Reply #13 on: March 31, 2010, 07:29:10 AM »
No you still do that in Php.

like, $userName or something...

it really doesn't matter how you define your variable names as long as you can understand it, im just used to this for php...

$u_firstName; //the users who the page belongs to
$v_firstName; //the user viewing

Of course, you could just use classes, but i never use them in php... only for computer programming :P

That's just me though, every person has their own tricks to organizing variables.