NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Quicker PHP scripts?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 05-09-2006, 11:28 AM THREAD STARTER               #1 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



Quicker PHP scripts?


Hi. I want to find out if these are quicker, slower or if there is no difference...

Would changing this:
PHP Code:
$message "<table>
????: NamePros.com http://www.namepros.com/programming/195573-quicker-php-scripts.html
                  <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 Code:
$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 Code:
$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=";
????: NamePros.com http://www.namepros.com/showthread.php?t=195573
        
$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 Code:
$echo .= "<td width=\"33%\" class=\"gray_small\" align=\"center\"><a href=\"image.php?qs=".urlencode($_SERVER['QUERY_STRING'])."&thumb="... 
??? Thanks
Tom
PoorDoggie is offline  
Old 05-09-2006, 11:39 AM   #2 (permalink)
Senior Member
 
Xyzer's Avatar
Join Date: Aug 2005
Location: United Kindom
Posts: 1,502
Xyzer is a name known to allXyzer is a name known to allXyzer is a name known to allXyzer is a name known to allXyzer is a name known to allXyzer is a name known to allXyzer is a name known to allXyzer is a name known to all
 


Tsunami Relief AIDS/HIV
The first one would be no different, im sorry but im not sure on the top one.


If i helped please donate rep.
Xyzer is offline  
Old 05-09-2006, 11:54 AM THREAD STARTER               #3 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



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
PoorDoggie is offline  
Old 05-09-2006, 12:01 PM   #4 (permalink)
NamePros Member
Join Date: Apr 2005
Posts: 117
mikesherov will become famous soon enoughmikesherov will become famous soon enough
 



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.
mikesherov is offline  
Old 05-09-2006, 02:19 PM THREAD STARTER               #5 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



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.

I have hundreds of lines of these type of coding, is there any way I can make the script more efficient?
PoorDoggie is offline  
Old 05-09-2006, 06:14 PM   #6 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
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 Code:
$time microtime();
$time explode(" "$time);
$time $time[1] + $time[0];
$start $time
Put this code below the code you are timing:
PHP Code:
$time microtime();
$time explode(" "$time);
$time $time[1] + $time[0];
????: NamePros.com http://www.namepros.com/showthread.php?t=195573
$finish $time;
$totaltime = ($finish $start);
????: NamePros.com http://www.namepros.com/showthread.php?t=195573
printf ("This page took %f seconds to load."$totaltime); 
Hope that helps,
Rhett.
BillyConnite is offline  
Old 05-09-2006, 08:28 PM   #7 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
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.
Dan is offline  
Old 05-09-2006, 09:41 PM   #8 (permalink)
Senior Member
 
david.amherst's Avatar
Join Date: Nov 2005
Location: Bay Area
Posts: 1,340
david.amherst is a name known to alldavid.amherst is a name known to alldavid.amherst is a name known to alldavid.amherst is a name known to alldavid.amherst is a name known to alldavid.amherst is a name known to all
 


Parkinson's Disease
Originally Posted by webmonkey
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.
david.amherst is offline  
Old 05-10-2006, 12:17 AM   #9 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,552
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
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
__________________
NameNewsletter.com - free lists of available domain names
ZoneFiles.net (beta) - ccTLD and gTLD droplists
sdsinc is offline  
Old 05-10-2006, 12:59 AM   #10 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by Dan Friedman
No PHP function makes a huge amount of time difference.
????: NamePros.com http://www.namepros.com/showthread.php?t=195573

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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-10-2006, 02:28 AM   #11 (permalink)
NamePros Regular
Join Date: Feb 2006
Posts: 584
jerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to all
 



Originally Posted by filth@flexiwebhost
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.
jerometan is offline  
Old 05-10-2006, 02:49 AM THREAD STARTER               #12 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



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?
PoorDoggie is offline  
Old 05-10-2006, 05:24 AM   #13 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Originally Posted by PoorDoggie
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,
????: NamePros.com http://www.namepros.com/showthread.php?t=195573

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
BillyConnite is offline  
Old 05-10-2006, 09:52 AM   #14 (permalink)
NamePros Member
Join Date: Apr 2005
Posts: 117
mikesherov will become famous soon enoughmikesherov will become famous soon enough
 



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 Code:
$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 Code:
$string.='<SELECT>';
????: NamePros.com http://www.namepros.com/showthread.php?t=195573
????: NamePros.com http://www.namepros.com/showthread.php?t=195573
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 Code:
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 Code:
$answer=intval($number_to_evaluate+.5); 
mikesherov is offline  
Old 05-10-2006, 10:16 AM   #15 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by jerometan
No, I strongly advise against it.
????: NamePros.com http://www.namepros.com/showthread.php?t=195573
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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-10-2006, 05:55 PM   #16 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
filth@flexiwebhost:

How is it being fast for small sites any different for more users? PHP doesn't get slower.
Dan is offline  
Old 05-10-2006, 09:35 PM   #17 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by Dan Friedman
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=195573

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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-12-2006, 05:44 AM   #18 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by Dan Friedman
filth@flexiwebhost:

How is it being fast for small sites any different for more users? PHP doesn't get slower.
just so happened came across this blog entry that talks about optimization of php:-

http://blogs.hackerscenter.com/dcrab/?p=9
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-12-2006, 09:19 AM   #19 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
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?
Dan is offline  
Old 05-12-2006, 04:11 PM   #20 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
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
Dan is offline  
Old 05-12-2006, 04:42 PM   #21 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Dan you missed the point of the post, Yes he showed the obvious slow functions to prove a point.

Languages such as php have many different ways of achieving the same end result and before deciding which to choose you should know which has the most processing power and execution time.
????: NamePros.com http://www.namepros.com/showthread.php?t=195573

For example say you have a mailling list of which 5000 people are subscribed. To send a message to each 1 using the mail() function requires php to open a connection 5000 times (it cloese the connection after each email has been sent). This of course courses overheads server wise as well as an increase of execution time. The same thing can be achieved by making a connection to an smtp server and sending the 5000 emails with only 1 connection, this will save processing power and execution time as it does not have to open and close a connection EVERY time a new message is being sent. Regardless of this the majority of people use the mail() function as 1) they know it is going to work and 2) they have no idea of the possible server overheads caused by it.
Peter is offline  
Old 05-12-2006, 04:56 PM   #22 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Obviously 5000 connections are worse than 1...
Dan is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:18 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger