strange performance of nested virtual methods

From: Markus Vogel (mail.mv_at_arcor.de)
Date: 03/16/05


Date: 16 Mar 2005 15:18:04 -0800

I wanted to decorate the features of a base class by declaring a
virtual method and deriving a new class, which overrides this method.
As I implemented this I found a strange performance behavior, that
occours as soon as I start nesting this concept.
I call this "strange performance behavior" because the extra cost of
calling time per nesting level is very irregular.

The following Table illustrates this.

nesting level extra calling time for the added level
1 500ms
2 1300ms
3 2400ms
4 500ms
5 500ms
6 500ms
7 500ms
. .
. .
. .

I found this behviour on three different machines but I can't find any
explaination.

Here comes what I'm doing

// This is my base class

        public class Service
        {
                int val;

                public virtual void SetValue(int val)
                {
                        this.val = val;
                }

        }

// This is my derived class (mind the constructor)

        public class ServiceDecorator : Service
        {
                Service baseService;

                public ServiceDecorator(Service baseService)
                {
                        this.baseService = baseService;
                }

                public override void SetValue(int val)
                {
                        baseService.SetValue(val + 10);
                }

        }

// And this is my test-class

        public class DecoratorTest
        {
                public static void Call_SetVal(Service service)
                {
                        const int count = 100000000;
                        int tick;

                        tick = Environment.TickCount;
                        for(int a=0; a<count; a++)
                        {
                                service.SetValue(a);
                        }
                        tick = Environment.TickCount - tick;
                        
                        Console.WriteLine(tick.ToString());
                }

                public static void RunTest()
                {
                        Service service = new Service();
                        // this takes 541ms
                        DecoratorTest.Call_SetVal(service);
                        
                        service = new ServiceDecorator(service);
                        // this takes 1051ms
                        DecoratorTest.Call_SetVal(service);
                        
                        service = new ServiceDecorator(service);
                        // this takes 2414ms
                        DecoratorTest.Call_SetVal(service);

                        service = new ServiceDecorator(service);
                        // this takes 4706ms
                        DecoratorTest.Call_SetVal(service);

                        service = new ServiceDecorator(service);
                        // this takes 5128ms
                        DecoratorTest.Call_SetVal(service);

                        service = new ServiceDecorator(service);
                        // this takes 5638ms
                        DecoratorTest.Call_SetVal(service);

                        service = new ServiceDecorator(service);
                        // this takes 6139ms
                        DecoratorTest.Call_SetVal(service);
                }

        }

Any Ideas?

Thanks in advance,
 Markus



Relevant Pages

  • RE: strange performance of nested virtual methods
    ... Timings are as follows: ... > virtual method and deriving a new class, ... > calling time per nesting level is very irregular. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: OT: CoolIris releases PicLens 1.6 for Firefox Mac & Win
    ... Class B inherits from A and overrides the virtual method foo introduced ... what if Foo is deleted from the base class? ... The base class doesn't have a Bar, ... you'll get a compile time error. ...
    (borland.public.delphi.non-technical)
  • a subtle change in behavior...
    ... 'public' access with 'public' and the code would still compile. ... the access of a virtual method defined in a base class. ... to create derived classes with the virtual method polymorphed as 'protected' ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Down casting objects
    ... Whats the best way to down cast obj to the correct type? ... better if you could simply declare a common abstract or virtual method in the base class that each derived class could override. ... then the best way to represent that is with a single overridden abstract or virtual method in the base class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: callable virtual method
    ... Does anyone know a way to write virtual methods that will raise an exception only if called without being overridden? ... Currently in the virtual method I'm checking that the class of the instance calling the method has defined that method as well. ... I'd like the base class to be virtual. ... Interface.method(self) # I want to process the cool stuff done my the base Interface ...
    (comp.lang.python)

Quantcast