Re: strange performance of nested virtual methods
From: Markus Vogel (mail.mv_at_arcor.de)
Date: 03/21/05
- Next message: john: "Re: Need to monitor a process"
- Previous message: Sitar: "Re: copy datatable"
- In reply to: Kismet: "Re: strange performance of nested virtual methods"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 22 Mar 2005 00:43:57 +0100
> I have done some further investigation.
> If you dissasemble the code generated and count machine cycles,
> there is very little overhead for the virtual calls when compared to the
> cycles
> required to execute the SetValue method in the first place.
> I am not sure what the point of your exercise is, but if you are worried
> about the overhead from calling a virtual method, don't.
Hi Kismet,
thanks for your investigations.
In my application I don't expect problems because of the issue I
discovered (the real world sample updates some Controls in the SetValue
method). It's just that I found this and I thought it might be of
interest to some others and for future concepts when performance really
matters.
> As a side note, you do realize that there is essentially no valid result
> obtained by running your code right? Since you call baseService.SetValue(),
> only the original instance "Service service".val gets assigned to anything.
> The ServiceDecorator Class instantiations inherit the val member, but never
> set it.
> As a result, there is no way to access your final computed value. I
I know, that there is no way to access the final value, but I wanted to
keep the provided example as simple as passible.
> I was a little surprised that the code as small as it is, didnt get inlined
> although that is probably because you cant inline virtual functions.
What does it mean? "the code ... didn't get inlined"
Unluckly I'm not very familliar with the internals.
Can you provide me a source, where I can read about this?
>
> My best guess as to why the timings are as they are is as follows:
> the cpu level 1 and 2 caches probably churn with 1 and 2 virtual calls,
> while after that, the cache holds the code required to execute the virtual
> methods.
> This could be verified by disabling the cache and rerunning the tests. (I
> am unable to disable the cache on my laptop which is where i am running the
> code).
Unluckly I'm using a laptop, too. And I didn't find a way to disable the
cache.
But I extended the code with another Decorator-Class "ServiceDecorator2"
which does exactly the same as "ServiceDecorator".
And the results show something that doesn't fit your "cache-theory"
public class ServiceDecorator2 : Service
{
Service baseService;
public ServiceDecorator2(Service baseService)
{
this.baseService = baseService;
}
public override void SetValue(int val)
{
baseService.SetValue(val + 10);
}
}
Furthermore I included a call to the "ServiceDecorator2" in my "RunTest"
method.
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);
//
//
// These are the two new lines
service = new ServiceDecorator2(service);
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);
}
This leads to the following results:
521
1031 => 510
1542 => 511 ("ServiceDecorator2")
2875 => 1333 (second instance of "ServiceDocorator")
5167 => 2292
6599 => 1432
7021 => 422
7570 => 549
Why is the extra time for the second Level now just 511ms instead of
1393ms as in the first sample?
Just as a second instance of "ServiceDecorator" is invoked, the extra
time is irregular as before!
And why does it go down to 422/549 again???
Can you find similar results ?
Thanks, Markus
- Next message: john: "Re: Need to monitor a process"
- Previous message: Sitar: "Re: copy datatable"
- In reply to: Kismet: "Re: strange performance of nested virtual methods"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|