473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expressions faster in Java ?..

I have made some comparision C# to Java RegularExpressi on. The problem was
to find out if the rule match some text.
Matching were done for precompiled regular expressions, in 100000 iterations
loop. Those loops were executed 11 times and average value of consumend time
was calculated. Below are codes for both classes.
And I found, that Java implementation is 2 to 5 times faster than C# (it
depends on complexity of expression).
Maybe my test were to simple? And Java made some optimisations, that the
code doesnt run (couse it really does nothing usefull)?

--
Pawe³

<<RegMatchTest. java>>
public class RegMatchTest
{

public static void main(String[] args) throws Exception
{
String pat[] = {"a*c?(d|f+) ", "g.*c?(r|d+ )"};
String word ;
long num = 100000;

File f = new File ("text.txt") ;

char buff[] = new char[(int)f.length()];
FileReader fr = new FileReader (f);
fr.read(buff);
word = new String (buff);
System.out.prin tln("Testing for "+num+" loops.");
long avgSum = 0 ;
for (int n = 0 ; n <= 10 ; ++n)
{
long t1 = System.currentT imeMillis();
new RegMatchTest(). Test1 (pat, word, num);
long t2 = System.currentT imeMillis();
System.out.prin tln("Elapsed time : " + (t2-t1) + " ms");
if (n > 0)
avgSum += t2-t1;
}
System.out.prin tln("\nAverage time : "+ (avgSum/10) +" ms");
}

boolean Test1 (String[] pat, String word, long len)
{
Pattern p[] = {Pattern.compil e(pat[0]),
Pattern.compile (pat[1])};

boolean b = false ;
for (int n = 0 ; n < len ; ++n)
{
Matcher m = p[n%2].matcher(word);
b = m.matches();
}
return b ;
}
<<class1.cs>>
class Class1
{
[STAThread]
static void Main(string[] args)
{
string[] pat = {@"a*c?(d|f+) ", @"g.*c?(r|d+)"} ;
string word ;
long num = 100000;

System.IO.Strea mReader tr = new System.IO.Strea mReader ("text.txt") ;
word = tr.ReadToEnd () ;

Console.WriteLi ne("Testing for "+num+" loops.");
long avgSum = 0 ;
for (int n = 0 ; n <= 10 ; ++n)
{
DateTime t1 = DateTime.Now;
new Class1().Test1 (pat, word, num);
DateTime t2 = DateTime.Now;
TimeSpan ts = t2 - t1 ;
Console.WriteLi ne("Elapsed time : " + (ts.TotalMillis econds) + " ms");
if (n > 0)
avgSum += (long)ts.TotalM illiseconds;
}
Console.WriteLi ne("\nAverage time : "+ (avgSum/10) +" ms");
}

bool Test1 (string[] pat, String word, long len)
{

Regex[] p = {new Regex (pat[0], RegexOptions.Co mpiled),
new Regex (pat[1], RegexOptions.Co mpiled)};
bool b = false ;
for (int n = 0 ; n < len ; ++n)
{
Match m = p[n%2].Match(word);
}
return b ;
}
}
Jul 21 '05 #1
1 2153
I noticed the same in the past. Regex seems to be poorly supported by C#.
They are real slow even when compiled. I heard there are people porting the
boost package to C# but haven't found it yet.

Yves

"pawel" <pa************ @interia.pl> schreef in bericht
news:ez******** ******@TK2MSFTN GP12.phx.gbl...
I have made some comparision C# to Java RegularExpressi on. The problem was
to find out if the rule match some text.
Matching were done for precompiled regular expressions, in 100000 iterations loop. Those loops were executed 11 times and average value of consumend time was calculated. Below are codes for both classes.
And I found, that Java implementation is 2 to 5 times faster than C# (it
depends on complexity of expression).
Maybe my test were to simple? And Java made some optimisations, that the
code doesnt run (couse it really does nothing usefull)?

--
Pawe³

<<RegMatchTest. java>>
public class RegMatchTest
{

public static void main(String[] args) throws Exception
{
String pat[] = {"a*c?(d|f+) ", "g.*c?(r|d+ )"};
String word ;
long num = 100000;

File f = new File ("text.txt") ;

char buff[] = new char[(int)f.length()];
FileReader fr = new FileReader (f);
fr.read(buff);
word = new String (buff);
System.out.prin tln("Testing for "+num+" loops.");
long avgSum = 0 ;
for (int n = 0 ; n <= 10 ; ++n)
{
long t1 = System.currentT imeMillis();
new RegMatchTest(). Test1 (pat, word, num);
long t2 = System.currentT imeMillis();
System.out.prin tln("Elapsed time : " + (t2-t1) + " ms");
if (n > 0)
avgSum += t2-t1;
}
System.out.prin tln("\nAverage time : "+ (avgSum/10) +" ms");
}

boolean Test1 (String[] pat, String word, long len)
{
Pattern p[] = {Pattern.compil e(pat[0]),
Pattern.compile (pat[1])};

boolean b = false ;
for (int n = 0 ; n < len ; ++n)
{
Matcher m = p[n%2].matcher(word);
b = m.matches();
}
return b ;
}
<<class1.cs>>
class Class1
{
[STAThread]
static void Main(string[] args)
{
string[] pat = {@"a*c?(d|f+) ", @"g.*c?(r|d+)"} ;
string word ;
long num = 100000;

System.IO.Strea mReader tr = new System.IO.Strea mReader ("text.txt") ;
word = tr.ReadToEnd () ;

Console.WriteLi ne("Testing for "+num+" loops.");
long avgSum = 0 ;
for (int n = 0 ; n <= 10 ; ++n)
{
DateTime t1 = DateTime.Now;
new Class1().Test1 (pat, word, num);
DateTime t2 = DateTime.Now;
TimeSpan ts = t2 - t1 ;
Console.WriteLi ne("Elapsed time : " + (ts.TotalMillis econds) + " ms");
if (n > 0)
avgSum += (long)ts.TotalM illiseconds;
}
Console.WriteLi ne("\nAverage time : "+ (avgSum/10) +" ms");
}

bool Test1 (string[] pat, String word, long len)
{

Regex[] p = {new Regex (pat[0], RegexOptions.Co mpiled),
new Regex (pat[1], RegexOptions.Co mpiled)};
bool b = false ;
for (int n = 0 ; n < len ; ++n)
{
Match m = p[n%2].Match(word);
}
return b ;
}
}

Jul 21 '05 #2

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

Similar topics

5
2716
by: DjDrakk | last post by:
I want to use headers to redirect the client if they didn't come from the correct page, but I have a problem with one page. If they leave a form field empty, they are redirected back to the page they came from with an error number and a 6 digit 1d number in the location bar which defines what field was left empty( order.php?prob=2&id=000001) My only problem is that when they repost, the $_SERVER contains the get variables and doesn't...
6
2052
by: Tony C | last post by:
I'm writing a python program which uses regular expressions, but I'm totally new to regexps. I've got Kuchling's "Regexp HOWTO", "Mastering Regular Expresions" by Oreilly, and have access to online stuff too. But I would like to find a mailing list or newsgroup where I can ask questions about regexps (when things don't work), not specifically dealing with Python. When I have Python-regexp questions, I'll post them here of course.
20
1916
by: Toby | last post by:
Could some tell how I could create a search replace Regular Express in .net where is would match MY_STRING_TO_BE_CONVERTED and replace with MyStringToBeConverted
3
2031
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing specialized code instead of an RE is almost always the better solution. Here is why.... RE's are complex. Sure it is one line of code, but it is on hell of a line. Some of my RE remind me of the obfuscated code contest winners, where one line of...
2
5107
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I have to use all the expressions seperately? Here are my regular expressions that check for valid email address and link Dim Expression As String =
1
338
by: pawel | last post by:
I have made some comparision C# to Java RegularExpression. The problem was to find out if the rule match some text. Matching were done for precompiled regular expressions, in 100000 iterations loop. Those loops were executed 11 times and average value of consumend time was calculated. Below are codes for both classes. And I found, that Java implementation is 2 to 5 times faster than C# (it depends on complexity of expression). Maybe my...
5
1451
by: Markus Innerebner | last post by:
Hello to everyone, Yesterday I tried long time to make a validation for a number format input field. As I am using Regex in Java I wrote following expression: String pattern = "((\\d+)((\\.)(\\d+)(\\,(\\d)+)?)?)|((\\d+)((\\,)(\\d+)(\\.(\\d)+)?)?)"; So the sequence of the chars of the input field could be?
19
2323
by: Davy | last post by:
Hi all, I am a C/C++/Perl user and want to switch to Python (I found Python is more similar to C). Does Python support robust regular expression like Perl? And Python and Perl's File content manipulation, which is better? Any suggestions will be appreciated!
13
7497
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can be accomplished with regular expressions this way, such as validating a mathematical expression or parsing a language with nested parens, quoting or expressions. Another feature I'm missing is once-only subpatterns and possessive quantifiers...
0
9711
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
9591
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
10594
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10087
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
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
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
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.