Regular Expression – Compiled vs Interpreted

Regular Expression gives the developer a clean and efficient method to parse strings. However, it comes at a cost – there is a inherent performance hit associated with regular expressions, especially if you are running some kind of long loops and parsing the string inside it. One way to tackle it is using compiled regular expressions, which .Net provides by using the RegexOptions.Compiled property. Let’s examine the performance of the Interpreted version of regular expression and the compiled version in a long loop.

We will consider a simple regular expression that is intended to parse a valid date.

void Main()
{
	string regex = @"^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$";
	var countArray = new int[]{100,1_000,10_000,1_00_000,10_00_000, Int32.MaxValue};
	foreach(var count in countArray)
	{
		RunLoop(regex,count,false);
		RunLoop(regex,count,true);
	}
}

void RunLoop(string regexStr,int maxCount,bool isCompiled)
{
	Stopwatch watch = new Stopwatch();
    watch.Start();
    for (int i = 0; i < maxCount; i++)
    {
		if(isCompiled)
        	Regex.IsMatch("23/2/2018", regexStr,RegexOptions.Compiled);
		else
			Regex.IsMatch("23/2/2018", regexStr);
				
	}

    watch.Stop();
    Console.WriteLine($"Time Ellapsed : {watch.Elapsed.Minutes}:{watch.Elapsed.Seconds}:{watch.Elapsed.Milliseconds}");
            
}

I ran the above code on different values for maxCount. You can view the comparison of Compiled Version Vs Interpreted Version below.

Iterations
Interpreted
Compiled
Int.MaxValue
26:13:629
15:44:589
10,00,000
00:00:793
00:00:450
1,00,000
00:00:79
00:00:57
10,000
00:00:09
00:00:07
1,000
00:00:01
00:00:02
100
00:00:00
00:00:02
As you can see, the more you need to use the same regular expression, Compiled Version is better. However, if the usage is very less, the compiler version turns expensive.

 

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s