473,408 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Why has noone mentioned performance?

Hi,

Amazing that noone has mentioned performance, especially since this is
almost certainly the key reason that methods are non-virtual by default!
Calling a virtual method is slower than calling a non-virtual method.
Objects with virtual methods also require more memory. Forcing programmers
to use slower-than-necessary methods (even when there's clearly no purpose
to it being virtual) is just poor design in a programming language.

Further discussion inline:

Eric Gunnerson [MS] wrote:
There are two different perspectives on library design, and which one
is better depends upon your perspective. It's a tradeoff between
flexibility and robustness.
Not really, see below:
If you value extensibility and flexibility, you will likely prefer
the Java approach, which says that everything can be extended unless
you explicitly say that it shouldn't be. If you value robustness
more, you will likely prefer the C# approach, which says that things
can be extended only if the class designer allowed it to be extended.
Surely making everything virtual is the opposite of flexibility? Flexibility
is *allowing* programmers to use non-virtual methods if they want. I can
easily get the Java approach by always using virtual or override, but I
don't have to.. *that's* flexible.

With the c# approach you have the best of both worlds -- extensibility if
you want it (and hence, flexibility).
In addition to the default behavior, there's also a philosophical
issue related to class design. Should you leave things virtual unless
you're sure there's a good reason for them not to be virtual, or
should you make them virtual only if you know of a good reason for
them to be virtual.


Like you implied earlier in your post.. you should use virtual methods where
you intend people to override them. Making a method virtual is as good
as -telling- the user that they can (and should) override the method. When
all methods are virtual people have no way of knowing which methods are
intended to be overridden, which can cause unforeseen problems.

I guess you could argue that making everything virtual gives extensibility
even where it wasn't intended.. but this just seems like you're working
around poor design to me.

Pete
Nov 15 '05 #1
3 1141
"Pete" <pv*****@gawab.com> wrote in
news:WW****************@newsfep2-win.server.ntli.net:
Amazing that noone has mentioned performance, especially since this is
almost certainly the key reason that methods are non-virtual by default!
Calling a virtual method is slower than calling a non-virtual method.
#define 'slower'. If you override a method, the compiler will make
sure the runtime will map the relative address of the overriding method in
the VTable of the class in which the original virtual method is located at
runtime. I don't see a slowdown there. Aren't you confusing static/non-
static methods?
Objects with virtual methods also require more memory. Forcing
programmers to use slower-than-necessary methods (even when there's
clearly no purpose to it being virtual) is just poor design in a
programming language.


Hmmm, only if the compiled code doesn't use VTables, otherwise there
is no difference. I'm pretty sure the compiled code uses a VTable variant.

FB

--
Solutions Design : http://www.sd.nl
My open source .NET Software : http://www.sd.nl/software
My .NET Blog : http://weblogs.asp.net/FBouma
-------------------------------------------------------------------------
Nov 15 '05 #2
Frans Bouma <pe**********@xs4all.nl> wrote:
"Pete" <pv*****@gawab.com> wrote in
news:WW****************@newsfep2-win.server.ntli.net:
Amazing that noone has mentioned performance, especially since this is
almost certainly the key reason that methods are non-virtual by default!
Calling a virtual method is slower than calling a non-virtual method.


#define 'slower'. If you override a method, the compiler will make
sure the runtime will map the relative address of the overriding method in
the VTable of the class in which the original virtual method is located at
runtime. I don't see a slowdown there. Aren't you confusing static/non-
static methods?


No, not necessarily. If a method is virtual, the address of the method
needs to be looked up in the vtable on every call, as it could be
different for different objects. If it's non-virtual, it will always be
the same, so that address can be injected directly into the code
stream. Non-virtual methods can also be inlined whereas virtual methods
can't.

Here's some sample code to show the difference:

// See http://www.pobox.com/~skeet/csharp/benchmark.html for
// how to run this code
using System;

public class VirtualSpeed
{
static int iterations=1000000000;

public static void Init(string[] args)
{
if (args.Length>0)
iterations = Int32.Parse(args[0]);
}

[Benchmark]
public static void TestVirtual()
{
VirtualSpeed foo = new VirtualSpeed();
for (int i=iterations; i > 0; i--)
foo.VirtualMethod();
}

[Benchmark]
public static void TestNonVirtual()
{
VirtualSpeed foo = new VirtualSpeed();
for (int i=iterations; i > 0; i--)
foo.NonVirtualMethod();
}

public virtual void VirtualMethod()
{
}

public void NonVirtualMethod()
{
}
}

Results:
C:\test>benchmark -runtwice
Benchmarking type VirtualSpeed
Run #1
TestVirtual 00:00:12.0973952
TestNonVirtual 00:00:03.4549680
Run #2
TestVirtual 00:00:12.0673520
TestNonVirtual 00:00:03.4549680

Note that this is one area where the HotSpot Java JIT has an advantage
over the MS CLR - because it can undo its optimisations, it can treat a
method as non-virtual until a class which overrides it is loaded. (I
wrote a test program to show that a while ago - it should be on
groups.google.com somewhere if you're interested.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
It's little wonder the Java JIT does that. If everything's virtual by
default, then you'd hope it would, otherwise the performance would be
shocking (given those numbers you have posted).

Niall

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Frans Bouma <pe**********@xs4all.nl> wrote:

Note that this is one area where the HotSpot Java JIT has an advantage
over the MS CLR - because it can undo its optimisations, it can treat a
method as non-virtual until a class which overrides it is loaded. (I
wrote a test program to show that a while ago - it should be on
groups.google.com somewhere if you're interested.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
12
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
12
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
4
by: Kory | last post by:
How do you stop a long running query with a SQLConnection or SQLCommand? Calling Close on either just waits until the query is done. Is there a way to stop it? The following on a long query ...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
1
by: jvn | last post by:
I am experiencing a particular problem with performance counters. I have created a set of classes, that uses System.Diagnostics.PerformanceCounter to increment custom performance counters (using...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.