Saturday, August 21, 2010

Cache in Regular Expressions


The chart illustrates the difference in performance between the code, which relies on repeatedly instantiating a Regex object with the same regular expression pattern to call an instance matching method, and the second, which calls a static matching method.

// BAD: Never reinstantiate the same object
Regex rgx = new Regex(pattern);
return rgx.IsMatch(input);

// GOOD: Take advantage of regex cache
return Regex.IsMatch(input, pattern);

The execution time of the first code is about 15 times the execution time of the second example. The difference in performance is due to the caching of regular expressions used in static method calls. Whereas the first example instantiates a regular expression object and converts the regular expression into opcodes in each of fourteen method calls (one for each element in the string array), the second example performs this conversion only once, on the first method call. Subsequently, it retrieves the interpreted regular expression.from the cache each time the expression is needed.

Only regular expressions used in static method calls are cached; regular expressions used in instance methods are not. The size of the cache is defined by the Regex.CacheSize property. By default, 15 regular expressions are cached, although this value can be modified if necessary. If the number of regular expressions exceeds the cache size, the regular expression engine discards the least recently used regular expression to cache the newest one.

Note that there is a breaking change in regular expression caching between versions 1.1 and subsequent versions of the .NET Framework. In version 1.1, both instance and static regular expressions are cached; in version 2.0 and all subsequent versions, only regular expressions used in static method calls are cached.

Saturday, July 31, 2010

Motivation


Itz great to see Jason Haley recommendation; my name is listed along with my favorite techie Somasegar, Scott, etc

Go Green Google


Blackle is a website powered by Google Custom Search, which aims to save energy by displaying a black background and using grayish-white font color for search results.

The concept behind Blackle is that computer monitors can be made to consume less energy by displaying much darker colors. Blackle is based on a study which tested a variety of CRT and LCD monitors. There is dispute over whether there really are any energy saving effects. This concept was first brought to the attention of Heap Media by a blog post, which estimated that Google could save 750 megawatt hours a year by utilizing it for CRT screens.[1][5] The homepage of Blackle provides a count of the number of watt hours claimed to have been saved by enabling this concept.

Statistics on power savings on the various LCD Models are listed in the attached image.

To read about blackle, plz visit at 'http://www.blackle.com/about/'

Sunday, July 25, 2010

Sealed Class


Sealed Class is the class which can not be inherited.the members/methods in the sealed class can not be derived from any other class. A compile-time error occurs if a sealed class is specified as the base class of another class. By default, structs are sealed; not classes in .NET

Here we see evidence of the small but measurable speedup allowed by the use of the keyword sealed in the C# programming language. The benchmark shows that for the very simplest interface methods, the sealed keyword can improve performance by about a third of a nanosecond per method invocation. For interface-heavy programs, the sealed keyword decoration can result in a speedup on all method calls with no downside.

~~~ Sealed class benchmark results (C#) ~~~       
See "Benchmark" section for the code compared.

TestA.GetNumber (regular): 2.490 ns
TestB.GetNumber (sealed): 2.162 ns [faster]

So, the keyword sealed provides a way to demand that the class not to be inherited from, but it is also useful for performance optimization.

Sunday, July 18, 2010

readonly preferance


C# has two versions of constants. They are compile timed 'const' and run timed 'readonly'. As named, const variables are replaced with the value of that constant in the object code. But readonly constants are evaluated at runtime; they are referenced with IL( Intermediate Language) generated variables not the value. Compile time constants can be used only for primitive types.

readonly for instance constants are used to store different values for each instance of a class type. Compile time const are static constants by definition
// compile time
public const int Millennium = 2000;

// run time
public static readonly int thisYear = 2010;

Advantage of using const over readonly is performance, where as readonly prefers the increased flexibility due to runtime compatability constants.

Thursday, July 8, 2010

MSDTC-Transaction Controller


On writing an .NET application with multiple LINQ insert/update statements TransactionScope is useful.
Letz say we are inserting EmployeeInfo table, then EmployeeSalaray table, followed by EmployeeAttendance table.
When the application completes all work it wants to perform in a transaction, Complete method informs transaction manager that it is acceptable to commit the transaction. Failing to call this method aborts the transaction. Isn't it cool?

But, I got an error '[System.Transactions.TransactionManagerCommunicationException] during TransactionScope execution. On analysis, it has been found Network access for Distributed Transaction Manager (MSDTC) has been disabled by default in XP. MSDTC is a transaction manager which permits client applications to include several different sources of data in one transaction, and which then coordinates committing the distributed transaction across all the servers that are enlisted in the transaction.

To resolve this issue, MSDTC is enabled via 'Control Panel->Administrative Tools->ComponentServices->MyComputer' MSDTC properties. You need to click on Security Configuration button under Transaction Configuration panel. In that section, enable 'Network DTC Access' After this setup, my application runs smoothly and TransactionScope is successful.