Older Contests
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP ContestsOlder Contests

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #16  
Old January 7th, 2003, 10:08 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Woohooo!

i was thinking exactly the same thing. 'this'.$is.'fast' should be parsed exactly like "this $is slow".

but again, maybe it is no preprocessing. and maybe php will never get preprocessing, because then zend accelerator couldn't sell.

i am realy curious how that would run with zend accelerator. even with optimizer...

Reply With Quote
  #17  
Old January 8th, 2003, 12:46 PM
mungk mungk is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Cincinnati, OH
Posts: 27 mungk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Woohooo!

I ran some more tests using the same basic loops that xs0 supplied and I got some very different results. I used an output buffer and flushed it after every loop since I didn't want to download all the output from this code, so that could have an affect on the runs.

Each of these loops 1000000 times:

0. echo 'qweqwe'; <- 9.5 seconds
1. echo "qweqwe"; <- 0.8 seconds
2. echo 'qweqwe '.$a.' qweqwe'; <- 84.9 seconds
3. echo "qweqwe $a qweqwe"; <- 5.1 seconds
4. echo 'qweqwe ', $a, ' qweqwe'; <- 2.8 seconds
5. echo "qweqwe ", $a, " qweqwe"; <- 2.8 seconds

Numbers 4 and 5 were tests that I added to test another method of echoing. As you can see, they did extremely well for me. As I understand it, the reason all of these have such different performance is in the way the strings are built.

In the cases where you embed a variable into a double-quoted string ("qweqwe $a qweqwe"), when PHP parses it, it scans through that string and builds one string in memory with the variable extrapolated. When it is written as "qweqwe " . $a . " qweqwe", PHP builds the first "qweqwe" string in memory, then concatenates the value of $a, then concatenates the last "qweqwe" string. That's why that test takes so long. It's doing a lot of string creation and resizing. (I think).

When you write the code with commas as in examples 4 and 5, PHP just outputs each one sequentially instead of concatenating them in memory then printing out the one string. This results in a huge speed up.

Any thoughts?

I will include the code I ran below:

php Code:
Original - php Code
  1.  
  2. <?php
  3.     set_time_limit(300);
  4.  
  5.     function getmicrotime() {
  6.         list($usec, $sec) = explode(" ",microtime());
  7.         return ((float)$usec + (float)$sec);
  8.     }
  9.  
  10.     $times = Array();
  11.     ob_start();
  12.     $test = 0;
  13.     $times[$test]['start'] = getmicrotime();
  14.     for ($a=0; $a<999999; $a++)
  15.         echo 'qweqwe';
  16.     $times[$test]['stop'] = getmicrotime();
  17.     ob_clean();
  18.  
  19.     $test++;
  20.     $times[$test]['start'] = getmicrotime();
  21.     for ($a=0; $a<999999; $a++)
  22.         echo "qweqwe";
  23.     $times[$test]['stop'] = getmicrotime();
  24.     ob_clean();
  25.  
  26.     $test++;
  27.     $times[$test]['start'] = getmicrotime();
  28.     for ($a=0; $a<999999; $a++)
  29.         echo 'qweqwe '.$a.' qweqwe';
  30.     $times[$test]['stop'] = getmicrotime();
  31.     ob_clean();
  32.  
  33.     $test++;
  34.     $times[$test]['start'] = getmicrotime();
  35.     for ($a=0; $a<999999; $a++)
  36.         echo "qweqwe $a qweqwe";
  37.     $times[$test]['stop'] = getmicrotime();
  38.     ob_clean();
  39.  
  40.     $test++;
  41.     $times[$test]['start'] = getmicrotime();
  42.     for ($a=0; $a<999999; $a++)
  43.         echo 'qweqwe ', $a, ' qweqwe';
  44.     $times[$test]['stop'] = getmicrotime();
  45.     ob_clean();
  46.  
  47.     $test++;
  48.     $times[$test]['start'] = getmicrotime();
  49.     for ($a=0; $a<999999; $a++)
  50.         echo "qweqwe ", $a, " qweqwe";
  51.     $times[$test]['stop'] = getmicrotime();
  52.     ob_clean();
  53.  
  54.     for ($lcv = 0; $lcv <= $test; $lcv++) {
  55.         echo "Test #$lcv:<br>n";
  56.         echo "Start: ", $times[$lcv]['start'], "<br>n";
  57.         echo "Stop: ", $times[$lcv]['stop'], "<br>n";
  58.         echo "Elapsed: ", $times[$lcv]['stop'] - $times[$lcv]['start'], " seconds<br>n";
  59.         echo "<hr>n";
  60.     }
  61.     ob_end_flush();
  62. ?>

Reply With Quote
  #18  
Old January 8th, 2003, 01:27 PM
xs0 xs0 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Ljubljana, Slovenia
Posts: 760 xs0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Woohooo!

I think there's a problem with your testing - on run 0, the output buffer needs to grow, so it takes long, run 1 is then considerably faster, because the buffer is already allocated to the final size. Run 2 grows the o.b. (again being very slow), and the rest of the runs are fast again.

I tested by creating 7 php files, each of which was timed with
> time php testn.php > /dev/null

The time was then calculated by adding sys and user times. This removes dependency on system load.

The tests were (with the same for loop as above):

1 - 2.8 secs: echo "qweqwe"
2 - 2.8 secs: echo 'qweqwe'
3 - 7.1 secs: echo "qweqwe ".$a." qweqwe"
4 - 9.7 secs: echo "qweqwe $a qweqwe"
5 - 7.1 secs: echo 'qweqwe '.$a.' qweqwe'
6 - 8.2 secs: echo "qweqwe ",$a," qweqwe"
7 - 8.2 secs: echo 'qweqwe ',$a,' qweqwe'

I guess the conclusions can be as follows:
- with no variables, both quotes are equally fast,
- inline variables are slower, perhaps becuase PHP parses such strings each time,
- echo "some","thing" is slower than "some"."thing", probably because in the first case, echo might get called twice, which seems to be slower than string concatenation...

The PHP used was 4.3.0RC3

Reply With Quote
  #19  
Old January 8th, 2003, 02:12 PM
mungk mungk is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Cincinnati, OH
Posts: 27 mungk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Woohooo!

I ran the tests again, this time in a different order, to see if your theory about the output buffering held true. From what I saw, that does seem to be the culprit. I was able to make different tests have much longer run times simply by positioning them in a different order.

My PHP machine is a windows machine so I have to vnc in order to have access to the command line. Of course, that's blocked by the firewall here at work so I'm not able to run "time" (or anything else to help get more accurate statistics) from here.

Reply With Quote
  #20  
Old January 8th, 2003, 05:40 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 2
RE: Woohooo!

So it seems that single/double quotes can be used interchangebly, and that variables should always be concat'ed, rather than placed inside strings.

I prefer using double quotes for server-side code and single quotes for client side, so it is good to know that double quotes can be used without sacrificing speed.

Do you think it is a good idea to stick strictly to double quotes in PHP code? Perhaps someone should write a style guide for PHP. Or maybe one already exists.

I recently read the Linux Kernel Style Guide, and it also applies mostly to PHP code. Is there an accepted style to add comments in code? I like the shell-style comments myself.

Reply With Quote
  #21  
Old January 8th, 2003, 10:59 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Woohooo!

only style guide for php i know of is one for pear. look http://pear.php.net (in docs you will find it..)

Reply With Quote
  #22  
Old January 10th, 2003, 10:37 PM
pickleman78 pickleman78 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Dallas,TX,USA
Posts: 582 pickleman78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to pickleman78
RE: Woohooo!

There is some thing on php coding style at webmasterbase.com somewhere i believe

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP ContestsOlder Contests > Woohooo!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway