General Community > Scripting Help

faster echo and loop

(1/5) > >>

Anguz:
I read this in the php-general newsgroup and found it interesting

I wanted to ask you ppl what you think about this, cause it's not to hard to change my way of coding to do it this way if it'll mean a faster script


--- Quote ---Chris Shiflett <mailto:shiflett@php.net>
    on Tuesday, September 30, 2003 11:39 AM said:

> The comma isn't concatenation; echo can take multiple arguments.

Oh ok, I get it.

> I've heard statements about passing multiple arguments to echo being
> faster than using concatenation, but every benchmark I've tried
> myself shows them to be nearly identical. Feel free to try it
> yourself and post your results. :-) I'd be curious to see if others
> reach the same conclusion.

I tried this, and I think the comma was just slightly faster than the
period. But then again I didn't always get consistent results because I
was trying this out on a machine that is not very fast so the speed at
which is processes something varies quite a bit.

My conclusion was that it's not fast enough to bother changing all my
code, or start writing in a new way.


Although during some testing I did find that writing loops in the
following way is faster than normal.

$counter = -1;
while(++$counter < $amount)
{
// do stuff
}

It made a big enough difference on my slow machine to merit me writing
as many loops in this way as I can. (I think.)



Chris.
--- End quote ---

Chris Cromer:
Using comma's instead of period's is faster, [Unknown] explained why it was faster... I will try to recap what he said but please note, this is off the top of my head.

He said that when you use this:

echo('test'.'test2');

It has to take the first string, and append it to the second string before printing it, which takes more php processing to do to cut it, and paste it to the end of the 'test' string.

When you use this:

echo('test','test2');

It is faster because it's printing both of them, instead of cuting and pasting it to the first string.

So it saves php processing by using the comma between the 2 strings.

Again, this is just a recap of something [Unknown] posted a while back, I am sure he can post it in much better wording than I could.

Anguz:
it makes enough sense to me like that :)

thx Chris

that's an easy change to do too, just find/replace ' . ' with ' , ' in an script should do the trick

and about the loop?


is there a compilation of tips and tricks to make faster scripts?

Chris Cromer:
Not too sure about the loop things havn't tried it, or even heard about it for that matter.

I recommend using single quotes at all times unless necessary to use double quotes, that makes things much faster because php doesn't try to parse the contents of the single quotes.

Anguz:
ok

about replacing the dot with a comma, it'd only be done in echo's right? not inside a function or something, so I guess just find/replace all the script like I mentioned above is wrong, but I could select the echo code and apply it to that only

I knew that single are faster than double quotes and I do it that way, thx for that too :)

Navigation

[0] Message Index

[#] Next page

Go to full version