Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > perl efficiency -- fastest grepping?

Reply
Thread Tools

perl efficiency -- fastest grepping?

 
 
Bryan Krone
Guest
Posts: n/a
 
      11-05-2004
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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      11-08-2004
In article <(E-Mail Removed)>, Bryan Krone
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl hash of hash efficiency. tak Perl Misc 28 08-07-2006 02:15 PM
perl efficiency -- fastest grepping? Bryan Krone Perl Misc 5 11-16-2004 06:39 PM
Fastest 5 mp Digital Camera ? Fastest 4 mp Digital Camera? photoguysept102004@yahoo.com Digital Photography 6 10-28-2004 11:33 AM
perl hash speed and memory efficiency odigity Perl Misc 1 05-27-2004 07:50 PM
Fastest Perl Interpreter Michael J. Astrauskas Perl Misc 14 11-20-2003 01:41 PM



Advertisments