NameSilo

Quicker PHP scripts?

Spaceship Spaceship
Watch

PoorDoggie

Soon to be RICHdoggie!VIP Member
Impact
18
Hi. I want to find out if these are quicker, slower or if there is no difference...

Would changing this:
PHP:
$message = "<table>
                  <tr>
                    <td rowspan=\"2\"><img src=\"images/icons/error.gif\" alt=\"Six - Error!\" /></td>
                    <td><b>Error</b></td>
                  </tr>
                  <tr>
                    <td>To increase relevancy Six only shows upto the <b>1000</b>th result (you requested $start)</td>
                  </tr>
                </table>";
to this:
PHP:
$message = "<table><tr><td rowspan=\"2\"><img src=\"images/icons/error.gif\" alt=\"Six - Error!\" /></td><td><b>Error</b></td></tr><tr><td>To increase relevancy Six only shows upto the <b>1000</b>th result (you requested $start)</td></tr></table>";
be any quicker (bearing in mind that code like this appears many places in my scripts)

and would this:
PHP:
$echo .= "<td width=\"33%\" class=\"gray_small\" align=\"center\"><a href=\"image.php?qs=";
        $echo .= urlencode($_SERVER['QUERY_STRING']);
        $echo .= "&thumb=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['Thumbnail']['Url']);
        $echo .= "&site=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['RefererUrl']);
        $echo .= "&title=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['Title']);
        $echo .= "&url=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['Url']);
        $echo .= "&size=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['FileSize']);
        $echo .= "&format=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['FileFormat']);
        $echo .= "&h=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['Height']);
        $echo .= "&w=";
        $echo .= urlencode($result['ResultSet']['Result'][$anum]['Width']);
        $echo .= "\"><img src=\"";
        $echo .= $result['ResultSet']['Result'][$anum]['Thumbnail']['Url'];
        $echo .= "\" /></a><br />";
be any quicker as one long $echo variable? ie:
PHP:
$echo .= "<td width=\"33%\" class=\"gray_small\" align=\"center\"><a href=\"image.php?qs=".urlencode($_SERVER['QUERY_STRING'])."&thumb="...

??? Thanks
Tom
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
The first one would be no different, im sorry but im not sure on the top one.


If i helped please donate rep.
 
0
•••
hmm.. helps but I'm too fond of you, and have to spread the rep a bit apparantly! sorry, can't say I didn't try! lol
 
0
•••
the time savings would be insignificant. the reason there are multiple statements is so the code is easier to read, and left right scrolling is unnecessary when reading.
 
0
•••
lol - I know why I wrote the script the way I did, I just wanted to know if I could speed the script up if I were to change it. :hehe:

I have hundreds of lines of these type of coding, is there any way I can make the script more efficient?
 
0
•••
In both sets above, neiter would make any difference, and if it did, it would be insignificant as mikesherov suggested.

But if you want to test it yourself, you can use the following code to test the pages loading time:

Put this code above the code you want to time:
PHP:
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$start = $time;
Put this code below the code you are timing:
PHP:
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf ("This page took %f seconds to load.", $totaltime);
Hope that helps,
Rhett.
 
0
•••
No PHP function makes a huge amount of time difference.

There are some functions which are slower than others but by .000x - .0x

My 137 line PHP code takes .001 to load.
 
0
•••
webmonkey said:
The first one would be no different, im sorry but im not sure on the top one.


If i helped please donate rep.
I don't believe that you are allowed to ask for rep.
 
0
•••
You should try with some benchmarking but I don't think it makes a difference these days because the PHP & ASP parsers are more powerful than they were in the past.
Anyway I prefer not to mix PHP with HTML because the code is really messy and hard to read after... not worth it :imho:
 
0
•••
Dan Friedman said:
No PHP function makes a huge amount of time difference.

There are some functions which are slower than others but by .000x - .0x

My 137 line PHP code takes .001 to load.


For a small site you are right it does not make a huge difference but for sites that server hundreds of thousands of users then it can make a huge difference.
 
0
•••
filth@flexiwebhost said:
For a small site you are right it does not make a huge difference but for sites that server hundreds of thousands of users then it can make a huge difference.

No, I strongly advise against it.
This makes code unreadable and hard to maintain.
This is especially so for big sites, so i disagree with filth@flexiwebhost.

If you really think that this will have speed improvements, do keep an original copy before squeezing them into 1 line, so that maintenance in future is easier.
 
0
•••
ok thanks a lot people. I just wanted to make my web site as fast as possible. Aside from getting a faster server there is no way to do this right?
 
0
•••
PoorDoggie said:
ok thanks a lot people. I just wanted to make my web site as fast as possible. Aside from getting a faster server there is no way to do this right?
Nope not really,

You can't really optmize the php statements you have, usually there can be a lot of optimization when it comes to mysql and things, but not basic statements, they're as fast as they can be :)
 
0
•••
Well, in principle, you can right more efficient code... but something like 1 assignment operation vs. 6 assignment operations is insignificant...

a little trick I like to use to increase my code efficiency:

When generating <SELECT> tags: using str_replace vs. if,then,else to select a default value... as follows:

instead of:

PHP:
$string.='<SELECT>';
foreach($set_of_values as $select_value){
    $string.='<OPTION value="'.$select_value.'"';
    if($select_value==$some_default_value)$string.=' SELECTED';
    $string.='>'.$select_value.'</OPTION>';
}

I do:

PHP:
$string.='<SELECT>';
foreach($set_of_values as $select_value){
    $string.='<OPTION value="'.$select_value.'">'.$select_value.'</OPTION>';
}
$string=str_replace('value="'.$select_value.'"','value="'.$select_value.'" SELECTED',$string);

both do the same thing, but instead of using an if statement over an over again, I harness the regex power of str_replace.

It's all about thinking as follows: What the best code to do rounding (in this example, I consider rounding to the nearest integer)?

one approach (the long way):
PHP:
if(substr($number_to_evaluate,strpos($number_to_evaluate,'.'),1)>=5){
    $answer=intval($number_to_evaluate)+1;
} else {
    $answer=intval($number_to_evaluate);
}

another approach (the easy way):
PHP:
$answer=intval($number_to_evaluate+.5);
 
0
•••
jerometan said:
No, I strongly advise against it.
This makes code unreadable and hard to maintain.
This is especially so for big sites, so i disagree with filth@flexiwebhost.

If you really think that this will have speed improvements, do keep an original copy before squeezing them into 1 line, so that maintenance in future is easier.


My post was a direct response of what Dan Friedman which was that no function sped up a script enough to worry about trying.
 
0
•••
filth@flexiwebhost:

How is it being fast for small sites any different for more users? PHP doesn't get slower.
 
0
•••
Dan Friedman said:
filth@flexiwebhost:

How is it being fast for small sites any different for more users? PHP doesn't get slower.

the changes are not as much an impact when the server is not being hit many times a second but if the server is being hit to near it's maximum then millisecond are a big impact, resources are freed up qicker.

Look around the internet regarding optimizing php scripts. Granted alot of sites it wouldn't make a huge difference with but if a site takes off then you would be starting on the wrong foot.
 
0
•••
0
•••
Yes, str_replace is faster than ereg/eregi/preg and including pointless files is slow. K.

Adding 1 and 5 in different functions.. I don't see the point of that, plus why not just make it one function?
 
0
•••
I just did a test between str_replace, ereg_replace, eregi_replace, and preg_replace.

I replaced all vowels with nothing in the sentence "Which function is faster? str_replace or eregi_replace" 1000 times.

str_replace: 0.0077230930328369 seconds
preg_replace: 0.0092120170593262 seconds
eregi_replace: 0.011637926101685 seconds
ereg_replace: 0.01211404800415 seconds
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back