IT Community - Software Programming, Web Development and Technical Support

optimizing the code

This is a discussion on optimizing the code within the PHP Programming forums, part of the Web Development category; Don't use a regex if you don't have to PHP has a rich set of string manipulation functions - ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-17-2007, 12:25 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default optimizing the code

Don't use a regex if you don't have to

PHP has a rich set of string manipulation functions - use them!

BAD: <? $new = ereg_replace("-","_",$str); ?>
GOOD:<? $new = str_replace("-","_",$str); ?>
BAD: <? preg_match('/(\..*?)$/',$str,$reg);?>
GOOD:<? substr($str,strrpos($str,'.')); ?>

Use References if you are passing large data structs around to save memory
There is a tradeoff here. Manipulating references is actually a bit slower than making copies of your
data, but with references you will be using less memory. So you need to determine if you are cpu or
memory bound to decide whether to go through and look for places to pass references to data instead
of copies.


Use Persistent Database connections

Some database are slower than others at establising new connections. The slower it is, the more of an
impact using persistent connections will have. But, keep in mind that persistent connections will sit
and tie up resources even when not in use. Watch your resource limits as well. For example, by
default Apache's

Using MySQL? Check out mysql_unbuffered_query()
Use it exactly like you would mysql_query(). The difference is that instead of waiting for the entire
query to finish and storing the result in the client API, an unbuffered query makes results available to
you as soon as possible and they are not allocated in the client API. You potentially get access to
your data quicker, use a lot less memory, but you can't use mysql_num_rows() on the result resource
and it is likely to be slightly slower for small selects.

Hey Einstein!
Don't over-architect things. If your solution seems complex to you, there is probably a simpler and
more obvious approach. Take a break from the computer and go out into the big (amazingly realistic)
room and think about something else for a bit.
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Register dll or ocx using through code in C# oxygen C# Programming 4 08-06-2008 12:58 AM
Zip code,Proximity kingmaker Database Support 3 10-11-2007 08:23 AM
I want the explanation for the below example code.. oxygen PHP Programming 1 07-26-2007 03:39 AM
JAVA code from PHP ramkumaraol PHP Programming 2 07-16-2007 11:54 PM
Perl Code from PHP sivaramakrishnan PHP Programming 1 07-16-2007 11:15 AM


All times are GMT -7. The time now is 03:45 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0