LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-29-2004, 06:35 AM   #1
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
optimize PHP string function


Hi,

I actually work on a function that parse names coming from a database. The purpose of the function is just reverse the last name and first name order, the original string is comma (and optional space) separated.

Examples :

Smith, Rob becomes Rob Smith
Mr Davidson becomes Mr Davidson
Barry,Betty becomes Betty Barry

I end by found two ways to do it but which is the more efficient ?

PHP Code:
// 1rst way

function parseName($name)
{
        if(
ereg(","$name)) { 
                return 
preg_replace("/(.*),\\s?(.*)/""\\\\2 \\\\1"$name); 
        }
        return 
$name;
}

//2nd way
function parseName($name)
{
        return 
join(" "array_reverse(split(", ?"$name)));

Thanks in advance for any suggestion

Last edited by Cedrik; 08-29-2004 at 06:36 AM.
 
Old 08-29-2004, 06:38 AM   #2
ZooL
Member
 
Registered: Aug 2004
Location: England
Distribution: Slackware 10.1
Posts: 41

Rep: Reputation: 15
Well if they both work exactly the same, then I'd say the second one because it has less instructions, meaning less cpu cycles.
 
Old 08-29-2004, 06:43 AM   #3
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Original Poster
Rep: Reputation: 242Reputation: 242Reputation: 242
The second has less instructions lines but more function call (3) instead of the first (2).

Thanks anyway for quick answer.
 
Old 08-29-2004, 06:47 AM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
I think the regular expression way takes more computing to find the match, where as the second just walks along a string til it finds the correct sequence.
 
Old 08-29-2004, 06:51 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Original Poster
Rep: Reputation: 242Reputation: 242Reputation: 242
Thanks Proud, you confirm what I thought ("regular expression can takes more computing").

I wanted to be sure because this function will be heavilly used in my scripts.

Have a good day
 
Old 08-29-2004, 07:33 AM   #6
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
Why don't you just test it, but running each one on like a million random names, and seeing which is faster?
 
Old 08-29-2004, 09:07 AM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Original Poster
Rep: Reputation: 242Reputation: 242Reputation: 242
Well, I just test my scripts on my machine and if they work, then I upload them to a distant server which run the production site, I don't want to do the test on distant server because it is actually in production use.

And I noticed that my machine and the server have not same behaviour at all

Last edited by Cedrik; 08-29-2004 at 09:08 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
please optimize this C function ewt3y Programming 10 08-12-2005 05:48 AM
Php image optimize script htmlcoder Programming 0 03-19-2005 03:23 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
PHP script/function to get 4-5 words around a keyword in a string(like google) rmanocha Programming 8 07-13-2004 06:19 AM


All times are GMT -5. The time now is 09:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration