perl efficiency -- fastest grepping?

Discussion in 'Perl' started by Bryan Krone, Nov 5, 2004.

  1. Bryan Krone

    Bryan Krone Guest

    I have a stream of data comming off a serial port at 19200. I am wondering
    what is the most efficient way to grep through the data in realtime? I have
    20 or so different strings I need to find. All of which are ~15 characters
    or less. Currently I'm using code that looks like this:

    forever loop
    {
    sysread the serial buffer into $newdata

    if( defined $newdata )
    {
    $inString =~ s/^.*(.{32})$/$1/o;
    $inString .= $newdata;
    }



    if( $inString =~ /.*ResetPF.*/o || $inString =~ /.*[gG][oO].*/o || $inString
    =~ /.*reset.*/o || $inString =~ /.*sysinit.*/o )
    {
    set some flag;
    }
    }
    Is there a more efficient way to grep for the strings to set some flag? This
    works pretty well but this is only 4 strings. I would like to add a lot
    more but the program slows down after 10 or more strings. Any ideas would
    be greatly appreciated.

    Thanks
     
    Bryan Krone, Nov 5, 2004
    #1
    1. Advertising

  2. Bryan Krone

    Jim Gibson Guest

    In article <>, Bryan Krone
    <> wrote:

    > I have a stream of data comming off a serial port at 19200. I am wondering
    > what is the most efficient way to grep through the data in realtime? I have
    > 20 or so different strings I need to find. All of which are ~15 characters
    > or less. Currently I'm using code that looks like this:
    >
    > forever loop
    > {
    > sysread the serial buffer into $newdata
    >
    > if( defined $newdata )
    > {
    > $inString =~ s/^.*(.{32})$/$1/o;
    > $inString .= $newdata;
    > }
    >
    >
    >
    > if( $inString =~ /.*ResetPF.*/o || $inString =~ /.*[gG][oO].*/o || $inString
    > =~ /.*reset.*/o || $inString =~ /.*sysinit.*/o )
    > {
    > set some flag;
    > }
    > }
    > Is there a more efficient way to grep for the strings to set some flag? This
    > works pretty well but this is only 4 strings. I would like to add a lot
    > more but the program slows down after 10 or more strings. Any ideas would
    > be greatly appreciated.


    The first thing to do is get rid of all the '.*' subpatterns. They do
    nothing for your matching. In other words, /string/ will match whenever
    /.*string.*/ matches. Wild cards can cause time-consuming backtracking.

    If your patterns are all fixed strings, you are better off using the
    index function instead of regular expressions. The only regular
    expression you show above is /[gG][oO]/. You are probably better off
    using the /i modifier to this regular expression and just searching for
    /go/. You can also convert each input string to lower-case and use
    index to search for 'go'.

    Another suggestion, if you need to stick with REs, is to use alternate
    patterns instead of separate searches. In other words,

    $inString =~ /resetpf|go|reset|sysinit/i

    may be faster than

    ($inString =~ /resetpf/i) || ($inString =~ /go/i) || ...

    Use the Benchmark module to investigate how each of these suggestions
    affects the speed of your program on your platform.

    FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
     
    Jim Gibson, Nov 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Fitzsimons

    Grepping with Python - script/prog required.

    John Fitzsimons, Jun 24, 2003, in forum: Python
    Replies:
    1
    Views:
    743
    John Fitzsimons
    Jul 1, 2003
  2. David A. Black

    [ANN] Grepper: object-oriented grepping

    David A. Black, Dec 24, 2008, in forum: Ruby
    Replies:
    1
    Views:
    138
    Tiago Nogueira
    Dec 24, 2008
  3. David A. Black
    Replies:
    3
    Views:
    118
    Florian Gilcher
    Jan 19, 2009
  4. mike
    Replies:
    15
    Views:
    195
    Anno Siegel
    May 21, 2004
  5. Bryan Krone

    perl efficiency -- fastest grepping?

    Bryan Krone, Nov 16, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    152
    Uri Guttman
    Nov 16, 2004
Loading...

Share This Page