Home > C# > C# Performance#3 – Generic vs strongly typed methods

C# Performance#3 – Generic vs strongly typed methods

It’s time to create another check. But before some info and explanation. Yesterday my friend asked me, why I create so dummy experiments, they are not pro and not useful. This was a good question for me and ascertainment. It’s true, that the check programs are not a production-applications, they are only simple methods with a timer and big loops, but they show differences between the same code (but with a little change inside). Maybe the results will be different if the test were more sophisticated. So take the experiments only to show some differences. Sometimes such an easy test can show how should You write the code and more :)

Going back…

The idea to make this simple check found me in my work. Developing some application written by a third company I saw many interesting lines of code. The most interesting for me were procedures responsible for serialization and deserialization. They provided two arguments – one with the Type parameter and one with object. What is more in early development I saw methods defined in each object making serialization (a class contained Setialization method and inside, the serialization functions). Developing and exploring the power of generic types I wrote two methods only with one parameter <T>. But i was interested in the processing time of my method (If is the same, better or worse than  the previous with two parameters). When I came home I wrote a program which compared the results.

The procedures:

public static string GenericSerialize<T>(T o) where T: class
    StringWriter sw = new StringWriter();
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    serializer.Serialize(sw, o);
    return sw.ToString();
} public static string Serialize(Type t, object o)
    StringWriter sw = new StringWriter();
    XmlSerializer serializer = new XmlSerializer(t);
    serializer.Serialize(sw, o);
    return sw.ToString();

And the results:

Strongly typed


avg 3130,55 (sum 62611) ms

avg 3159,75 (sum 63195) ms

I was little disappointed with the results because the strongly typed procedure was a bit faster.  Every procedure was executed 20 times in a loop with 65534 iterations. As You can see the strongly typed serialization procedure is approximately 0,6 sec faster than the generic. It’s not much, but always is.
But the profit is the ease of use generic methods, rather writing Serialize(typeof(MySimpleClass), myClass) You can write Just Serialize(myClass).
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: