Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

C# 4.0 Dynamic Type - Reflection Speed Test

Tuesday, 8 June 2010 19:04 by ugur

At first view, you can think that dynamic types come with C# 4.0 are only used for to make a type written outside the .Net environment applicable in .Net environment. However, we can achieve important performance earnings when we use Dynamic types instead of Reflection types while coding by using Reflection types. Therefore, it makes sense to use Dynamic types instead of Reflection types in the applications developed by using C# 4.0.  

In the example below, we have two classes named Music and Video that apply IMedia interface. In this simple console application I prepared, I created Music and Video objects and I called generic PlayMedia<T> method that performs Play operations of these objects. Depending on the scenario, in order to make the use of reflection or dynamic type necessary, I designed this application that any object that includes Play method can be used in PlayMedia method. The Play method of media object which’s type is uncertain during the compiling is called by using dynamic type in the lines numbered 20 and 21 of PlayMedia method. If you want to test the usage of Reflection types, you can turn the lines 20 and 21 to comment lines and remove the comment tags in the line numbered 19. First, I see the results by operating only the line 19. Then, when I operate the lines 20 and 21, the difference between the results is highly impressive :)

Reflection is trying to reach the members included in a type such as method and field by analyzing the structure of the type. However, using dynamic type can reach the result faster because dynamic type achieves the intended member directly without analyzing the type. Anyway, you can see the speed difference if you test this issue as given below.

The class diagram of the types used in the example is given below.

    1 class Program

    2 {

    3     static void Main(string[] args)

    4     {

    5         Music m = new Music { FileName = "amazing.mp3" };

    6         Video v = new Video { FileName = "avatar_fragman.wmv" };

    7 

    8         PlayMedia<Music>(m);

    9         PlayMedia<Video>(v);

   10         Console.ReadLine();

   11     }

   12 

   13     static void PlayMedia<T>(T media)

   14     {

   15         DateTime d1, d2;

   16         d1 = DateTime.Now;

   17         for (int i = 0; i < 1000000; i++)

   18         {

   19             //media.GetType().GetMethod("Play").Invoke(media, null);

   20             dynamic d = media;

   21             d.Play();

   22         }

   23 

   24         d2 = DateTime.Now;

   25         TimeSpan t = d2 - d1;

   26 

   27         Console.WriteLine(t.TotalSeconds);

   28     }

   29 }

As can be seen from the test results, the process done upon 1.000.000 record by using reflection type takes 1.85 seconds whereas when I use dynamic type, it takes average 0.17 seconds. Therefore, we can achieve the result 10 times faster when we use dynamic type. Of course, different results can be obtained in different scenarios but we can say that dynamic type is clearly faster than reflection types. It is obvious that, dynamic type is a better choice in terms of performance in these types of processes.     

Tags:   , ,
Categories:   C#
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Insert, Update & Delete in LINQ to SQL

Saturday, 9 August 2008 23:07 by ugur

Insert, Update & Delete in LINQ to SQL As you know, we can make queries among collection based objects in .NET environment with the language integrated queries named LINQ. The main purpose of these queries is to simplify the complicated processes by writing simple Select queries just as in T-SQL; instead of using foreach loops and if-else control structures. The main issue point of LINQ is to provide making simple and practical queries among enormous and complicated data objects (entity objects) in an application and to reduce one of the heavy weights of the software developers in extended projects.

One of the most frequently asked questions related to LINQ to SQL is how to carry out Insert, Update and Delete processes. I think it is not needed to be said that; it can not be made queries with the key words Insert, Update and Delete in LINQ statementsJ Thus, if the purpose is to work with the data objects in LINQ to SQL, somehow we need to perform these kind of actions. LINQ to SQL Classes (.dbml) files provide us to transform the data objects from database straightly to our application as classes. For example, if we form Product table included in Northwind database as data class in our application, the Products property included in DataContext object will return the records in Products table as the collection. Some methods of this property will help us to perform query statements like Insert, Update and Delete.

With InsertOnSubmit statement we can perform Insert act and with DeleteOnSubmit statement we can perform Delete act. Okay, but where is Update act? We will perform Update act by changing the values of the record or records return as LINQ query result. In order to transfer the results of these three acts to the database, it will be adequate to execute the SubmitChanges method of DataContext object. If you examine the code lines given below, you can see how to insert, delete and update data in LINQ to SQL. The Northwind object included in code snippet is our DataContext object.

Product addPr = new Product() { ProductName="Acer Aspire 5100", UnitPrice=1290, UnitsInStock=15, CategoryID=9 };

northWind.Products.InsertOnSubmit(addPr); // A new product added to insert list

 

Product updatePr = northWind.Products.First(u => u.ProductID == 69);

guncellenecekUrun.ProductName = "HP Pavilion 3355"; // ProductName property has changed for selected product

 

Product deletePr = northWind.Products.First(u => u.ProductID == 79);

northWind.Products.DeleteOnSubmit(deletePr); // Selected product added to delete list

 

northWind.SubmitChanges(); // Changes(insert/update/delete) has sent to database

Tags:   ,
Categories:   C#
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed
 
Add to Technorati Favorites