Fw: PHP URL issues

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



One more example of a questionable benchmark, not wrong but I wonder (as I was doing in my previous) whether it is really representative.

There are two main ways to handle strings, one is to malloc exactly the required memory for each string and keep moving characters around from one chunk of memory to another. If you append a single character to a string, you would measure both, add their lengths plus whatever overhead your data representation requires, malloc that much memory, move the characters from each source and then free the memory occupied by the first.

The other way to do it is to malloc memory in more or less fixed sizes and include in the header of your variable a length field. With plenty of memory available nowadays, few still choose the first option. Appending a few characters does not require any new memory allocation nor freeing it up. The later method depends on how good where your statistics (more damned lies once again) about the average string length, but if you get it right, appending to a string ( .= operator) becomes as fast as appending to a StreamBuffer, as found in other languages with separate Strings and Streams classes. Some languages do not even require string space to be contiguous so that when one block is used up you don't have to get a bigger one and move everything to the new place, the blocks comprising the string are linked in a list. That's one reason why lots of languages now prefer 'immutable strings', it is no longer possible to treat the string as an array of characters since they cannot be accessed via a simple offset.

This benchmark is probably exercising more the symbol table lookups and memory allocation functions than the actual concatenation, since it is allocating and freeing up lots of space for really tiny strings that don't take much of that space at all. Whatever difference there is in the actual string operation might be clouded under other factors.

Regardless, I do prefer to use single quotes and, as I mentioned elsewhere, I just echo the bits and pieces as soon as I am able, reasoning that the output buffer is truly a stream buffer and since the echo is a language construct (no function call overhead), it should be faster than managing memory to hold strings. As I said, though, the performance of one option against others varies so much in different trials under different conditions that I feel foolish just to show them.

Then, not even logic works fine if you mean to define what is faster and what is not. Sometimes the developers, using profilers and such tools, find a very slow spot and they get to optimize it to such an extent that it works faster than its un-optimized relative, which under equal circumstances would have actually been faster.

Using single quotes for PHP strings frees me the double quotes for HTML attribute values without any need to escape them. I have defined CRLF and TAB as constants which I can redefine at any time to an empty string if I no longer care my HTML output to be readable. Since I prefer to keep all values in their native PHP format up to the last moment, and those values have to be passed either through htmlentities(), number_format(), money_format() or strftotime() on output, variable interpolation is out of the question anyway.

Satyam



----- Original Message ----- From: "Steve" <steve@xxxxxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Wednesday, March 14, 2007 3:25 PM
Subject: Re:  PHP URL issues


I personally have not unfortunately. A good friend of mine is also a developer who initially told me about it when I first began coding. Because I don't want to stick with the he-said, she-said approach, I did a quick google search and came up with this link:

http://spindrop.us/2007/03/03/php-double-versus-single-quotes/

There's several other pages saying similar things if you do look around.

Even the performance different isn't significant, it will add up. If something like this is known to help ever so slightly with execution time and it's something you can teach yourself to do subconsciously, then I feel it's definitely worthwhile to pursue.

""Richard Lynch"" <ceo@xxxxxxxxx> wrote in message news:1980.67.184.122.32.1173863242.squirrel@xxxxxxxxxxxxxxxx
On Tue, March 13, 2007 9:31 am, Steve wrote:
This may or may not help, but here's a few things to note:

1) I would avoid placing variable output in double quoted strings.
While not
important for smaller scripts, doing a large number of outputs like
this
causes a decent performance hit. In fact, I wouldn't use double quotes
ever
in php. Instead, strive for something like:

echo '<b><a href="test.php?term='.$letter_value.'">'.$letter_value.'
&nbsp;</a></b>';

Have you benchmarked this, or can you provide a link to others'
benchmarks demonstrating this perofmrnace hit?...

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.11/721 - Release Date: 13/03/2007 16:51



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux