Pages: [1]   Go Down
Author Topic: PHP Efficiency Tests  (Read 419 times)
hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« on: March 24, 2010, 02: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 Cheesy
hope i helped you guys Cheesy
Report to moderator   Logged

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #1 on: March 24, 2010, 06:21:57 AM »

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.
Report to moderator   Logged

hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« Reply #2 on: March 24, 2010, 01: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.
Report to moderator   Logged

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #3 on: March 24, 2010, 07:12:48 PM »

Isee. But hat were the tests you ran? did you just store the variables and see how long that took or what?
Report to moderator   Logged

hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« Reply #4 on: March 24, 2010, 11:39:41 PM »

Yes, i recorded the time it took to do it... and no i didn't use a stopwatch Tongue

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

Report to moderator   Logged

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #5 on: March 25, 2010, 08:00:39 AM »

i know you would loose precision, but why didn't you truncate or round the microtime difference?
Report to moderator   Logged

hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« Reply #6 on: March 25, 2010, 11:43:00 AM »

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.
Report to moderator   Logged

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #7 on: March 26, 2010, 06:51:32 AM »

oh, right... hmmm...
Report to moderator   Logged

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #8 on: March 30, 2010, 01: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:
<?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
?>

Report to moderator   Logged

hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« Reply #9 on: March 30, 2010, 03:47:49 AM »

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.
Tongue
Report to moderator   Logged

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #10 on: March 30, 2010, 05:19:09 AM »

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.
Report to moderator   Logged

hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« Reply #11 on: March 30, 2010, 08:11:25 AM »

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.
Report to moderator   Logged

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

RoseKnight
Sr. Member
****

Karma: +0/-0
Offline Offline

Posts: 52


« Reply #12 on: March 30, 2010, 09:53:25 AM »

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.
Report to moderator   Logged

hotnoob
PhpBar.isgreat.org
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 113


WWW
« Reply #13 on: March 30, 2010, 10:29:10 PM »

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 Tongue

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

http://phpbar.isgreat.org
Feel free to check out my forums for more tutorials and help Cheesy

Pages: [1]   Go Up
Print
 
Jump to:  

Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC | Theme Kani By Fussilet

Page created in 0.071 seconds with 21 queries.