473,385 Members | 1,922 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,385 software developers and data experts.

Regular Expressions faster in Java ?..

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 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.println("Testing for "+num+" loops.");
long avgSum = 0 ;
for (int n = 0 ; n <= 10 ; ++n)
{
long t1 = System.currentTimeMillis();
new RegMatchTest().Test1 (pat, word, num);
long t2 = System.currentTimeMillis();
System.out.println("Elapsed time : " + (t2-t1) + " ms");
if (n > 0)
avgSum += t2-t1;
}
System.out.println("\nAverage time : "+ (avgSum/10) +" ms");
}

boolean Test1 (String[] pat, String word, long len)
{
Pattern p[] = {Pattern.compile(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.StreamReader tr = new System.IO.StreamReader ("text.txt") ;
word = tr.ReadToEnd () ;

Console.WriteLine("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.WriteLine("Elapsed time : " + (ts.TotalMilliseconds) + " ms");
if (n > 0)
avgSum += (long)ts.TotalMilliseconds;
}
Console.WriteLine("\nAverage time : "+ (avgSum/10) +" ms");
}

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

Regex[] p = {new Regex (pat[0], RegexOptions.Compiled),
new Regex (pat[1], RegexOptions.Compiled)};
bool b = false ;
for (int n = 0 ; n < len ; ++n)
{
Match m = p[n%2].Match(word);
}
return b ;
}
}
Jul 21 '05 #1
1 2098
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**************@TK2MSFTNGP12.phx.gbl...
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 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.println("Testing for "+num+" loops.");
long avgSum = 0 ;
for (int n = 0 ; n <= 10 ; ++n)
{
long t1 = System.currentTimeMillis();
new RegMatchTest().Test1 (pat, word, num);
long t2 = System.currentTimeMillis();
System.out.println("Elapsed time : " + (t2-t1) + " ms");
if (n > 0)
avgSum += t2-t1;
}
System.out.println("\nAverage time : "+ (avgSum/10) +" ms");
}

boolean Test1 (String[] pat, String word, long len)
{
Pattern p[] = {Pattern.compile(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.StreamReader tr = new System.IO.StreamReader ("text.txt") ;
word = tr.ReadToEnd () ;

Console.WriteLine("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.WriteLine("Elapsed time : " + (ts.TotalMilliseconds) + " ms");
if (n > 0)
avgSum += (long)ts.TotalMilliseconds;
}
Console.WriteLine("\nAverage time : "+ (avgSum/10) +" ms");
}

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

Regex[] p = {new Regex (pat[0], RegexOptions.Compiled),
new Regex (pat[1], RegexOptions.Compiled)};
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
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...
6
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...
20
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
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...
2
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...
1
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...
5
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 =...
19
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...
13
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.