Friday, February 18, 2011

Measure execution time in C#

Sometimes developers need to measure execution time of task. This methodics could be useful when is important to compare two or more approaches and choose the best. .Net Framework contains Stopwatch class that can measure elapsed time for one interval, or total elapsed time across multiple intervals.
For more information navigate to: Stopwatch Class.

Following example demonstrates how to use Stopwatch Class to determine execution time for an application. Using this example you can see differences in efficiency by calling Contains method of StringCollection and HashTable.


Hashtable hs = new Hashtable();
StringCollection sc = new StringCollection();
            
for (int i = 0; i < int.MaxValue/1000; i++)
{
    hs.Add(i.ToString(), i);
    sc.Add(i.ToString());
}

Random rnd = new Random();
Stopwatch timer = new Stopwatch();

int val = rnd.Next(int.MaxValue / 1000);

timer.Start();
if (sc.Contains(val.ToString()))
{
    Console.WriteLine("Contains");
}
timer.Stop();

Console.WriteLine(timer.ElapsedTicks);
timer.Reset();

timer.Start();
if (hs.Contains(val.ToString()))
{
    Console.WriteLine("Contains");
}
timer.Stop();

Console.WriteLine(timer.ElapsedTicks);

Thursday, February 17, 2011

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 7 (Implementing globalization, drawing, and text manipulation functionality in a .NET Framework application)

Format data based on culture information.
Enhance the user interface of a .NET Framework application by using brushes, pens, colors, and fonts.
Enhance the user interface of a .NET Framework application by using graphics, images, bitmaps, and icons.
Enhance the user interface of a .NET Framework application by using shapes and sizes.
Enhance the text handling capabilities of a .NET Framework application (refer System.Text namespace), and search, modify, and control text in a .NET Framework application by using regular expressions.

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 6 (Implementing interoperability, reflection, and mailing functionality in a .NET Framework application)

Expose COM components to the .NET Framework and the .NET Framework components to COM.
Call unmanaged DLL functions in a .NET Framework application, and control the marshaling of data in a .NET Framework application.
Implement reflection functionality in a .NET Framework application (refer System.Reflection namespace), and create metadata, Microsoft intermediate language (MSIL), and a PE file by using the System.Reflection.Emit namespace.
Send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery from a .NET Framework application.

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 5 (Improving the security of .NET Framework applications by using the .NET Framework security features)

Implement code access security to improve the security of a .NET Framework application.
Implement access control by using the System.Security.AccessControl classes.
Implement a custom authentication scheme by using the System.Security.Authentication classes.
Encrypt, decrypt, and hash data by using the System.Security.Cryptography classes.
Control permissions for resources by using the System.Security.Permission classes.
Control code privileges by using System.Security.Policy classes.
Access and modify identity information by using the System.Security.Principal classes.

Wednesday, February 16, 2011

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 4 (Implementing serialization and input/output functionality in a .NET Framework application)

Serialize or deserialize an object or an object graph by using runtime serialization techniques.
Control the serialization of an object into XML format by using the System.Xml.Serialization namespace.
Implement custom serialization formatting by using the Serialization Formatter classes.
Access files and folders by using the File System classes.
Manage byte streams by using Stream classes.
Manage the .NET Framework application data by using Reader and Writer classes.
Compress or decompress stream information in a .NET Framework application and improve the security of application data by using isolated storage.

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 3 (Embedding configuration, diagnostic, management, and installation features into a .NET Framework application)

Embed configuration management functionality into a .NET Framework application.
Create a custom Microsoft Windows Installer for the .NET Framework components by using the System.Configuration.Install namespace, and configure the .NET Framework applications by using configuration files, environment variables, and the .NET Framework Configuration tool (Mscorcfg.msc).
Manage an event log by using the System.Diagnostics namespace.
Manage system processes and monitor the performance of a .NET Framework application by using the diagnostics functionality of the .NET Framework 2.0.
Debug and trace a .NET Framework application by using the System.Diagnostics namespace.
Embed management information and events into a .NET Framework application.

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 2 (Implementing service processes, threading, and application domains in a .NET Framework application)

Implement, install, and control a service.
Develop multithreaded .NET Framework applications.
Create a unit of isolation for common language runtime in a .NET Framework application by using application domains.

MCTS .NET Application Development Foundation - 70-536 – Objectives List Part 1 (Developing applications that use system types and collections)

Manage data in a .NET Framework application by using the .NET Framework 2.0 system types
Manage a group of associated data in a .NET Framework application by using collections
Improve type safety and application performance in a .NET Framework application by using generic collections
Manage data in a .NET Framework application by using specialized collections
Implement .NET Framework interfaces to cause components to comply with standard contracts
Control interactions between .NET Framework application components by using events and delegates