Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment

Locations of visitors to this page

Latest Articles

Move to top
Testing performance issues with reflection
Posted By Sarin on Jan 15, 2013     RSS Feeds     Latest Hinduism news

I like the concept of c# reflection so much that I generally prefer to use it under all circumstances. Using the reflection makes your task easier and faster. However there is a downside to using reflection. It increases the execution time. I believed that the increase in execution time may be 50% to 100 % and hence preferred to use it for small data. However being lazy and in the conquest of using shortcuts, once I used reflection for comparatively huge data and noticed the execution time to be extremely slow. There, I decided to analyze the performance issues of using reflection.
Let me first give you an example on how I was using reflection to retrieve data. Since I was working on a silver light project, I had to use List<ClassName> for binding data to my data grid. Setter properties inside my class were same as the database column names. Out of laziness, Instead of manually assigning each property the value of the database column, I used reflection to assign the data to a property.
For Example, I have a class Student as

    class Stud
        public string StudentID  get; set;
        public string Name  get; set;
        public string Batch  get; set;

And suppose my database query retrieve list of all students from a table as “Select StudentID, Name, Batch from student”. Using the query dataset, we would loop through all the rows and will create a list of student class as.  
    List<Stud> lstStud = new List<Stud>();
            foreach(Datarow row in stud.Rows)
                Stud g = new Stud();
                g.StudentID = row["StudentId"];
                g.Name = row["Name"];
                g.Batch = row["Batch"];

This is very easy for small set of data but what if you have more than 20 columns and what if you need to do the above type of assignments for more than 20 tables. 20* 20=400 assignment statements. Very boring task? Isn’t it? If you are a lazy person like me, then you will try to escape from such boring task by using reflection.
For the above scenario, I can use reflection to achieve the same functionality as
                Stud ga = new Stud();
                PropertyInfo[] pi = ga.GetType().GetProperties();
            foreach(Datarow row in stud.Rows)
                foreach (PropertyInfo p in pi)
                  p.SetValue(ga, row[p.Name], null);
In above piece of code, I loop through each rows of the database result and assign it to properties of the class Stud by looping through each properties of the class using its property name. Now Even if I have 20, 100 or 1000 columns, my code will remain the same the above small chunk of code will work as long as the column name matches with the property name.
I can add one more level of reflection by dynamically creating the class object and using this class object for assigning values to my property. Below is the code.
            foreach(Datarow row in stud.Rows)
               dynamic typeClass = typeof(Stud);
                dynamic objectClass = Activator.CreateInstance(typeClass);
                PropertyInfo[] pi = typeClass.GetProperties();
                foreach (PropertyInfo p in pi)
                    p.SetValue(objectClass, "sar", null);

Now let use analyze the performance of the above code. I have developed a small program which executes each of the above code for fifty thousand iterations. This will help us to determine the execution time of each code thereby helping us in comparing the relative performance in the different types of solutions to the same problem.  

Above results shows that the first approach takes 6 ms while the second approach takes 533 and 784 ms respectively. From above, we see that the reflection is 50 to 200 times slower than the normal way of coding and hence, I can conclude that we should do away from reflection unless it is very essential. Please find the attached code for the above program.

Note: Images used on this website are either a production of Bhaktivedanta Book Trust(, Iskcon Foundation or were found in google search under "Free to use and share". If any of the images presented here violates copyright issues or infringes anyone copyright or are not under "Fair use", then please bring it to our notice. Read Disclaimer for more.

Share this to your friends. One of your friend is waiting for your share.
Related Articles
Get comma separated result of a database query
Hide or unhide data using Visibility ValueConverter class
Get All Tables in a Database
Need and advantages of using computed columns
Calling web service asynchronously using jquery ajax
Generating unique random numbers and date
Decade of war between politicians over ram setu
How music affects plant growth
Working with silver light datagrid
When to use optional parameters of creating database statement

Post Comment