Connecting Tech Pros Worldwide Help | Site Map

how to improve perf. ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 02:28 AM
erickrefener
Guest
 
Posts: n/a
Default how to improve perf. ?

Hi,

I made a script which analyse very long strings and I need to make it
work differently depending of the 5 first chars of it.

I already do :

switch (true){
case ( ereg("12345", $data) ):
print '$data est un chaine 12345';
break;
case ( ereg("54321", $data) ):
print '$data est un chaine 54321';
break;
default:
print 'invalid data';
}

cause those first chars order doesn't matter, it can't be found in the
same order again ...

I know that there are lots of way to do this with php but could you tell
me which would the fastest way (talking about performance) to do it ?
(in order to make php not to analyse the entire string but only those 5
first chars ....)

thanks a lot,


erick


  #2  
Old July 17th, 2005, 02:28 AM
Tom Thackrey
Guest
 
Posts: n/a
Default Re: how to improve perf. ?


On 16-Jan-2004, erickrefener <erick@spamitoudina.com> wrote:
[color=blue]
> I made a script which analyse very long strings and I need to make it
> work differently depending of the 5 first chars of it.
>
> I already do :
>
> switch (true){
> case ( ereg("12345", $data) ):
> print '$data est un chaine 12345';
> break;
> case ( ereg("54321", $data) ):
> print '$data est un chaine 54321';
> break;
> default:
> print 'invalid data';
> }
>
> cause those first chars order doesn't matter, it can't be found in the
> same order again ...
>
> I know that there are lots of way to do this with php but could you tell
> me which would the fastest way (talking about performance) to do it ?
> (in order to make php not to analyse the entire string but only those 5
> first chars ....)
>
> thanks a lot,[/color]

Please cross-post in the future.

switch (substr($data,0,5))
{
case '12345':
...
case '54321':
...
}

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)
  #3  
Old July 17th, 2005, 02:28 AM
Chung Leong
Guest
 
Posts: n/a
Default Re: how to improve perf. ?

First of all, what is the point of writing an if-else-if statement as a
switch statement?

As noted in the PHP manual preg_match() is faster than ereg. The problem
with you regular expression is you're telling it to search to whole text.
Put a ^ at the beginning to specify match at the beginning of the string.

if(preg_match("/^12345/", $data)) {
print '$data est un chaine 12345';
}
else if(preg_match("/^54321/", $data)) {
print '$data est un chaine 54321';
}
else {
}

Of course, the assumption here is that the actual regular expressions are
more complicated that a straight string comparison.

Uzytkownik "erickrefener" <erick@spamitoudina.com> napisal w wiadomosci
news:QLYNb.7204$c1.1005915@news20.bellglobal.com.. .[color=blue]
> Hi,
>
> I made a script which analyse very long strings and I need to make it
> work differently depending of the 5 first chars of it.
>
> I already do :
>
> switch (true){
> case ( ereg("12345", $data) ):
> print '$data est un chaine 12345';
> break;
> case ( ereg("54321", $data) ):
> print '$data est un chaine 54321';
> break;
> default:
> print 'invalid data';
> }
>
> cause those first chars order doesn't matter, it can't be found in the
> same order again ...
>
> I know that there are lots of way to do this with php but could you tell
> me which would the fastest way (talking about performance) to do it ?
> (in order to make php not to analyse the entire string but only those 5
> first chars ....)
>
> thanks a lot,
>
>
> erick
>[/color]


 

Bookmarks

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 On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.