|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#16
|
|||
|
|||
|
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... |
|
#17
|
|||||
|
|||||
|
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:
|
|
#18
|
|||
|
|||
|
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 |
|
#19
|
|||
|
|||
|
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. |
|
#20
|
|||
|
|||
|
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. |
|
#21
|
|||
|
|||
|
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..)
|
|
#22
|
|||
|
|||
|
RE: Woohooo!
There is some thing on php coding style at webmasterbase.com somewhere i believe
|
![]() |
| Viewing: Codewalkers Forums > PHP Contests > Older Contests > Woohooo! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|