Click here to Skip to main content
12,625,694 members (37,446 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Virtual Methods can cause performance overheads

, 18 Oct 2011 LGPL3
Rate this:
Please Sign up or sign in to vote.
Often we developers do not realize the pros and cons of a feature in a language.

Often we developers do not realize the pros and cons of a feature in a language. Yet we still do use them at our wimps and fancies. Although at first you might think I am talking non-sense, any good programmer should also think twice about a feature before accepting it for granted.

In this article, I will talk about Virtual Methods in inheritance trees causing performance overheads some times or when used extensively.

Even though you can place the override keyword in every method, internally the compiler converts all of them to virtual. Hence at run time, the CLR has to execute one more instructure and has to look in method tables as which concrete types method has to be called.  This extra calculation or searching pattern do adds a bit of performance hit (Thanks to Qwertie for pointing the mistake), if it is done often by the CLR. Hence the virtual methods do not get inlined by the compiler. 

Let's see some sample code:


As you can see from the above code, although it looks very simplistic in nature, there is a small trick to this article's interests.

Let's see the main method's IL:


As you can see from the IL, in comparison with the Main() code, I have declared:

SecondChild d = new ThirdChild()

But in the IL, the compiler is calling virtually Base.DoWork(). Since the run time sees the virtual keyword in the method definition it tries to search the hierarchy in the child classes from this base class. This way it traverses till the leaf node in the tree till it no longer finds the overriding method/in other words another virtual method at least in this example. Otherwise, it will traverse till the RHS class encountered which is declared in the code, i.e., ThirdChild() here. Beyond that, it does not care to search, since that’s how it has been declared in the code.

In the above case, since the method in the ThirdChild class has a new definition, it hooks up the call for SecondChilds DoWork(). If you remove the new keyword from the ThirdChild class DoWork(), and provide the override keyword, then the ThirdChild class method shall be called. Since it is the most derived method available in the inheritance tree.

Now you might have understood how CLR runs through the whole long subclass tree structure. Hence it is better to avoid too many virtual methods in your production code, folks :)

Thanks :)

P.S.: Your valuable comments/votes are well appreciated. 


One of the codeproject reader has requested me to provide a cost comparison. I wonder why i did not care/missed out to provide this information when i wrote the article. I apologize to all the readers for this mistake. So here i am providing the requested information:

So for the test purpose, i wrote the following code:(Thanks Qwertie for pointing out the mistake)

    class Base
        public virtual void Hello() { }

    class Child1 : Base
        public override void Hello() { }
    class Child2 : Child1
        public override void Hello() { }

    class Child3 : Child2
        public override void Hello() { }

    class SomeClass
        public  void Hello() { }

    sealed class Program  //: IDisposable
        static void Main(string[] args)
            Base b = new Child3();

            Stopwatch sp = Stopwatch.StartNew();

            SomeClass c = new SomeClass();

            sp = Stopwatch.StartNew();

Now as per this code, the output you get in terms of ticks shows you the performance or inheritance call ticks is more than directly calling a method. The output is shown below:


Yes the difference is very less, but upon increasing the hierarchy as well as frequent usage, this difference grows. Even i checked JR book on the same and its true.

One more point worth mentioning here is, not only the virtual methods performance is slow, its also depends on how we use it. Thanks to Kabwla suggestion that even using the inheritance tree do also matters in performance issue i.e new usage over inheritance tree structure.

What i mean is, in the above test code in place of SomeClass c = new SomeClass() if i had placed Child4 c = new Child4() then the performance of this would be very less even though Child4 is in the inheritance tree plus its method is a virtual method. But run time gets smart in knowing its not a cast to base type as in Base b = new Child4() and hence it directly place a call. But most of developers do not use child class instance directly i.e Child4 c = new Child4() , so i can broadly say virtual inheritance is costly. 


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging ->

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Qwertie19-Oct-11 6:42
memberQwertie19-Oct-11 6:42 
GeneralRe: My vote of 3 Pin
zenwalker198519-Oct-11 8:29
memberzenwalker198519-Oct-11 8:29 
GeneralRe: My vote of 3 Pin
Qwertie19-Oct-11 10:10
memberQwertie19-Oct-11 10:10 
GeneralRe: My vote of 3 Pin
zenwalker198519-Oct-11 17:50
memberzenwalker198519-Oct-11 17:50 
GeneralMy vote of 1 Pin
Qwertie18-Oct-11 9:45
memberQwertie18-Oct-11 9:45 
GeneralRe: My vote of 1 Pin
zenwalker198518-Oct-11 17:51
memberzenwalker198518-Oct-11 17:51 
QuestionPlease demonstrate the cost Pin
Kabwla.Phone18-Oct-11 3:55
memberKabwla.Phone18-Oct-11 3:55 
GeneralRe: Please demonstrate the cost Pin
Kabwla.Phone18-Oct-11 4:52
memberKabwla.Phone18-Oct-11 4:52 
AnswerRe: Please demonstrate the cost Pin
zenwalker198518-Oct-11 5:47
memberzenwalker198518-Oct-11 5:47 
GeneralMy vote of 4 Pin
Sadaqat Hussain18-Oct-11 1:41
memberSadaqat Hussain18-Oct-11 1:41 
GeneralRe: My vote of 4 Pin
zenwalker198518-Oct-11 4:33
memberzenwalker198518-Oct-11 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 18 Oct 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid