473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is 1 large Regex faster than 3 smaller ones?

Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?

Thanks!
Erik

Jun 3 '07 #1
2 1697
In article <11**********************@p47g2000hsd.googlegroups .com>,
erikcw <er***********@gmail.comwrote:
Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?

Thanks!
Erik
For a classic regex, the answer is one big one. Matching against a regex
takes time proportional to the number of characters of input. One big
regex will probably consume more memory, and may be slower to compile, but
it should run faster.

On the other hand, there are a lot of things that pattern maching libraries
accept these days under the guise of being a "regex" which are not strictly
regexes in the classic sense.

From a purely practical point of view, if your input is only 200 words,
it's likely that the search time will be insignificant no matter what you
do.
Jun 3 '07 #2
On Jun 4, 4:52 am, erikcw <erikwickst...@gmail.comwrote:
Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?
Taking your question literally: we have no idea how long it might take
you to write alternative versions of source code.

Guessing that you mean "Which would run faster?": both should run over
a 200-word text file in imperceptibly different time (unless of course
you make a mistake), and the time should be tiny compared to the time
required to open the file (again unless of course you make a mistake).

I'd suggest that as a learning exercise you do both, check the
accuracy of both (text e.g. matching only 2 out of 3, sought strings
in different orders, etc), and then and only then worry about the time
it takes.

HTH,
John

Jun 3 '07 #3

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

Similar topics

4
by: Dag Sunde | last post by:
Just wondering if anyone have looked into this? How to split up ones JavaScript library? A lot of very specific (and small) .js files, or a few larger files. I'm thinking about load-time...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
104
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
13
by: sevenz24 | last post by:
So i have my images set up like this : http://cgi.ebay.com/Tippmann-98-paintBall-Marker-Gun-Paint-Ball_W0QQitemZ250274334261QQihZ015QQcategoryZ47248QQssPageNameZWDVWQQrdZ1QQcmdZViewItem Scroll...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.