[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 05-09-2006, 10:28 AM   #1 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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>
                  <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=";
        
$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, 10:39 AM   #2 (permalink)
Senior Member
 
Xyzer's Avatar
 
Join Date: Aug 2005
Location: United Kindom
Posts: 1,506
90.70 NP$ (Donate)

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 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, 10:54 AM   #3 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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, 11:01 AM   #4 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

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, 01:19 PM   #5 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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, 05:14 PM   #6 (permalink)
 
BillyConnite's Avatar
 
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,107
47.00 NP$ (Donate)

BillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant future

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];
$finish = $time;
$totaltime = ($finish - $start);
printf ("This page took %f seconds to load.", $totaltime);
Hope that helps,
Rhett.
__________________
<?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?>
BillyConnite is offline  
Old 05-09-2006, 07:28 PM   #7 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 08:41 PM   #8 (permalink)
Senior Member
 
david.amherst's Avatar
 
Join Date: Nov 2005
Location: Bay Area
Posts: 1,341
182.40 NP$ (Donate)

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
Quote:
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-09-2006, 11:17 PM   #9 (permalink)
Domains my Dominion
 
sdsinc's Avatar
 
Join Date: Aug 2005
Location: Web 1.0
Posts: 6,284
1,095.94 NP$ (Donate)

sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute

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
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
__________________
Buy now - MassDeveloper.com $500
sdsinc is offline  
Old 05-09-2006, 11:59 PM   #10 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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
Quote:
Originally Posted by Dan Friedman
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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-10-2006, 01:28 AM   #11 (permalink)
NamePros Regular
 
Join Date: Feb 2006
Posts: 588
1,620.95 NP$ (Donate)

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 all


Quote:
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, 01:49 AM   #12 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie 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, 04:24 AM   #13 (permalink)
 
BillyConnite's Avatar
 
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,107
47.00 NP$ (Donate)

BillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant future

Wildlife Parkinson's Disease Parkinson's Disease
Quote:
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,

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
__________________
<?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?>
BillyConnite is offline  
Old 05-10-2006, 08:52 AM   #14 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

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>';
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, 09:16 AM   #15 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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
Quote:
Originally Posted by jerometan
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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-10-2006, 04:55 PM   #16 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 08:35 PM   #17 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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
Quote:
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.

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, 04:44 AM   #18 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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
Quote:
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, 08:19 AM   #19 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 03:11 PM   #20 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 03:42 PM   #21 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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.

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, 03:56 PM   #22 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 01:41 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85