473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

perl efficiency -- fastest grepping?

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
Jul 19 '05 #1
1 4606
In article <1p************ ********@comcas t.com>, Bryan Krone
<br********@com cast.net> 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|rese t|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.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
4777
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
4
1893
by: Xah Lee | last post by:
20050207 text pattern matching # -*- coding: utf-8 -*- # Python # suppose you want to replace all strings of the form # <img src="some.gif" width="30" height="20"> # to # <img src="some.png" width="30" height="20"> # in your html files.
7
2145
by: Ajar | last post by:
I want to write a program which will automatically login to my ISPs website, retrieve data and do some processing. Can this be done? Can you point me to any example python programs which do similar things? Regards, Ajar
2
1453
by: David Busby | last post by:
List, Which of the may Perl Modules on CPAN should I use for PostgreSQL. I'd prefer the fastest/most up to date module. Which is it or do I have to dig through them all? David Busby Systems Engineer busby@pnts.com
2
1760
by: jeffrey.bigham | last post by:
Hello, I'm writing a program that needs to read input line by line and analyze each, and it needs to be as efficient as possible. I wrote the following sample program that works, but is 10 times slower than a Perl equivalent on a large input (~212 Mb). There has to be something wrong - can anyone help me fix it? I've included both at the bottom of my post and I compile with g++ -03 cin_test.C Thanks!
19
2922
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
35
19458
by: lnatz | last post by:
Hi, Is there an equivalent to the perl command chomp in C? And if there is no exact equivalent command, how would I go about removing the "\n" at the end of a stdin? Thank you, Natalie
2
2821
by: Thormod Johansen | last post by:
Hi, I have just read the article about iterators at http://www.oreillynet.com/pub/a/network/2005/10/18/what-is-iterator-in-c-plus-plus.html?page=last and I am wondering about what is ment by the sentence "list does not provide random access iterators, though, not because it's impossible to implement, but because it can't be implemented efficiently." As I understand it, a vector is a single linked list and list is a double linked list....
12
4290
by: disappearedng | last post by:
Hi all, I am currently planning to write my own web crawler. I know Python but not Perl, and I am interested in knowing which of these two are a better choice given the following scenario: 1) I/O issues: my biggest constraint in terms of resource will be bandwidth throttle neck. 2) Efficiency issues: The crawlers have to be fast, robust and as "memory efficient" as possible. I am running all of my crawlers on cheap pcs with about 500...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.