Tuesday, April 28, 2009

DateTime.Now Vs. System.Diagnostics.Stopwatch

Occasionally all of us, as programmers would like to see how fast or how slow a C# function can execute. I am sure, most of us must have used DateTime.Now for the measurements. Roughly on these lines,

DateTime startTime = DateTime.Now;

// Some Execution Process

DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);

Now, does DateTime.Now accurately measure performance? No, it does not. DateTime.Now, according to MSDN has a resolution of ~16ms, which means it is not very precise.

Another approach would to use system tickcount as kept by Environment.TickCount. This keeps the number of milliseconds since the computer was started. Unfortunately it is stored as a signed int, so every 49 days it is actually counting backwards. We can compensate for this by just taking off the most siginificant bit.

Another better approach is to use Stopwatch. System.Diagnostics offers Stopwatch that automatically checks for high precision timers. You can check if the Stopwatch is using high-precision timers by checking via Stopwatch.IsHighResolution() method.

Stopwatch swTimer = Stopwatch.StartNew();
//Some execution process here

Watch for the difference in the code results

protected void Page_Load(object sender, EventArgs e)
DateTime sStartTime, sEndTime;
TimeSpan tsTotalTimeTaken;

sStartTime = DateTime.Now;
for (long lCount = 0; lCount < 100; lCount++)
}//for(int iCount=0; iCount<100; iCount++)

sEndTime = DateTime.Now;

Stopwatch swTimer = Stopwatch.StartNew();

for (long lCount = 0; lCount < 100; lCount++)
}//for(int iCount=0; iCount<100; iCount++)


tsTotalTimeTaken = sEndTime.Subtract(sStartTime);

Response.Write("Elaspsed Time according to DateTime.Now(): " + tsTotalTimeTaken.Milliseconds + "
Response.Write("Elapsed Time according to StopWatch(): " + swTimer.Elapsed.Milliseconds);


Friday, April 3, 2009

Wanna be Macbie

Mac Dream
MacBook has been on my 'Desire List' for sometime now. Everytime I think of indulging myself with the sleek user interface that Apple offers, I push the idea out thinking that its going to be a difficult shift for a ASP.NET developer. Is it really that difficult?

ForeWord: This blog post comes out with no PC vs. Mac argument.This is just research on things needed for a ASP.NET developer treading into Mac waters.

Questions Galore
How can I compile C# code on Mac? Will I have to learn to write code on Objective-C? Should I start using the Cocoa Framework instead of the .NET Framework? Will I still be able to use AJAX.NET on Mac? These are the questions that ring loud and clear in any .NET programmer's mind.

I try to seek answers to these and wish for a Mac soon! :)

BootCamp comes packaged with Mac OS X. This software assists users to install Windows XP and Windows Vista while leaving all the Mac data in place while it creates a separate partition on your drive for Windows. BootCamp even lets you start up the Mac using either of the operating systems.

Parallels and VMWare
Parallels and VMWare are virtualization softwares that help break down the wall between Mac and a PC. Both these products offer a seamless desktop environment between Mac OS X and PC applications.

These virtualization softwares do come with certain performance penalties. See Jeff Atwood ponder problems and discuss solutions.

Decide between Parallels/VMWare and BootCamp


Mono is a open-source development initiative to develop an open source, UNIX version of Microsoft.NET development platform. Mono includes a CLI (Common Language Infrastructure) virtual machine, a class library that works on CLR and a compiler for C# language.

Everything seems okay, but aren't we still missing something that .NET programmers cannot live without?? Yeah, the Visual Studio has got you hooked onto it - you will not know how much you love this tool until you start missing it! I see the Microsoft guys smiling :)

Here comes MonoDevelop. MonoDevelop is a IDE designed for C# and other .NET languages. Developed code can also be ported on unix machines. Take a look at Screenshots for MonoDevelop, they are really promising.

Mono and AJAX.NET?
Microsoft's ASP.NET AJAX Control Toolkit works on Mono, and is open source (Ms-PL). That doesn't preclude the use of jQuery, though. There are actually several third party AJAX libraries and control toolkits that support Mono, and many are compatible or work in conjunction with ASP.NET AJAX.

You .Net developers, ready for a Mac? :)