Re: how to improve perf. ?

From: Chung Leong (chernyshevsky_at_hotmail.com)
Date: 01/17/04


Date: Fri, 16 Jan 2004 19:59:51 -0500

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



Relevant Pages

  • Re: Lots of design issues needed.
    ... One possibility would be to replace the switch statement with an array ... > int showMenu() ... > int binarySearch(VPerson a, const string& x, const compare comp) const ...
    (alt.comp.lang.learn.c-cpp)
  • Re: member references
    ... then the next string would be the member name and sub member names etc. ... then use reflection to set/get the value. ... If you have a situation in which you think a switch statement does make sense, then rather than a plain index, you should probably use an enumeration. ... Why not just post a code sample that illustrates the basic idea. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: switch statement with the string being tested
    ... I have about 50 string values that can be in a string ... The switch statement seems to be a better choice then the ... int func; ... Visit http://www.ecomstation.de the home of german eComStation ...
    (comp.lang.c)
  • Re: Creating a vCal-interpreter
    ... explode would like regexps for delimiter. ... The date part will be the first chars in the second item of the array. ... In a string i have many rows, I want to extract some data from one of ... extract the information ...
    (comp.lang.php)
  • Re: Help needed in solving C-errors in Linux (gcc)
    ... > When i tried to compile this test driver for an actual conversion library ... "multi-character character constant". ... you can't use a string in a switch statement. ...
    (comp.os.linux.development.apps)