Page 1 of 2

[solved] virtual functions or function pointers?

Posted: Wed Jan 19, 2011 12:34 am
by Void Mapper
In my game engine all the graphics related classes extend from a pure virtual base class that requires each of them to implement a draw function. I got the idea of using function pointers instead when I tried to recreate the engine in C. Would it be better to use function pointers or virtual functions in this instance (or in general even)?

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 1:58 am
by XianForce
Well, I may be wrong, but I would say function pointers would be FASTER, simply because of the overhead associated with v-tables... But with that being said, I don't think it's significant enough to change from using virtual functions to function pointers...

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 7:41 am
by Ginto8
If either is actually faster, it's by a negligable amount. You have a game running on a CPU that's at least 1.5 GHz faster than your program needs, often even more, and you're concerned about function pointers vs virtual functions? You're way over-concerned with a very minor problem.

Although, to answer your question, function pointers would be faster because vtables internally use function pointers.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 8:30 am
by N64vSNES
Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 10:24 am
by Ginto8
N64vSNES wrote:Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
Once again you open your mouth and spew shit ;) Doing something regardless of overhead is basically saying "This looks pretty, I don't give a shit that it runs like a leg amputee on crutches." Also, OOP doesn't always make things pretty. Sometimes OOP is ugly. OOP is a tool, and you should use it as such. Just because a crowbar can be used to hammer in a nail doesn't mean it should.

What I was trying to say was not that you shouldn't worry about overhead; I was saying you should weigh overhead against code prettiness, and that tiny amounts of overhead shouldn't bother you. I was saying that he should worry about getting it to work before optimizing the hell out of it. Also, you shouldn't be so worried about design that you ignore performance. Coding is an art, and as in all art, balance is everything.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 11:49 am
by Void Mapper
Ginto8 wrote:If either is actually faster, it's by a negligable amount. You have a game running on a CPU that's at least 1.5 GHz faster than your program needs, often even more, and you're concerned about function pointers vs virtual functions? You're way over-concerned with a very minor problem.

Although, to answer your question, function pointers would be faster because vtables internally use function pointers.
The reason I care is because I'm using 1.60 GHz processor and I'm stuck using openGL 1.1, which if you didn't know means that I don't have the benefit of things (as far as I can tell) like VBOs, display lists, or shaders. It will be a while before I can get a decent computer.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 12:10 pm
by N64vSNES
Ginto8 wrote: Once again you open your mouth and spew shit
...Good start.
Ginto8 wrote: Doing something regardless of overhead is basically saying "This looks pretty, I don't give a shit that it runs like a leg amputee on crutches.
You're exaggerating the overhead of a virtual function a teeeney weeeney bit there arn't you?
Ginto8 wrote: Also, OOP doesn't always make things pretty. Sometimes OOP is ugly. OOP is a tool, and you should use it as such. Just because a crowbar can be used to hammer in a nail doesn't mean it should.
True, but if the is a easier OO way to do somthing and you don't take this approach then why the hell would you even wanting to use a OO laungauge?


Now before we start a war, notice what I said here:
I wrote:C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
Why ignore the one of the most foundemental components of OOP?

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 1:25 pm
by Ginto8
Void Mapper wrote:The reason I care is because I'm using 1.60 GHz processor and I'm stuck using openGL 1.1, which if you didn't know means that I don't have the benefit of things (as far as I can tell) like VBOs, display lists, or shaders. It will be a while before I can get a decent computer.
Even with such a restrictive system as this, the overhead of virtual functions vs function pointers should be negligible. If you really want to know, write 2 small programs, one that calls a virtual function 10 thousand times, another that calls a function pointer that many times. Then, use a program similar to unix's "time" to determine which is better.

Now N64vSNES, I wasn't actually talking about virtual functions. I was talking about using OOP whenever possible regardless of performance. Second, C++ is not an OOP language. It is a language WITH OOP. If you want an example of an OOP language, look at Java or C#, which force OOP to a point. C++ is, by definition, a multi-paradigm language that supports numerous different types of use, from functional programming with templates to procedural to, yes, OOP. However, I maintain that OOP is a tool, and you are saying to use it as a crutch. It is no cure-all, and oftentimes is NOT the best idea. That's not to say that it can't be a great idea, but it should not take a complete backseat to performance. Balance performance and design, and you will see that OOP isn't always necessary and, in some cases, is absolutely retched.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 1:26 pm
by Falco Girgis
N64vSNES wrote:Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
That's idiocy. Why don't you go enable RTTI on every project then? Why don't you go enable runtime exceptions? Why are you even writing in C++, if languages like Java and C# have greater overhead and offer more object-oriented power?

And to correctly answer your question, vtables are one operation slower than function pointers.

Invoking a function is one operation.

Invoking a function via a function pointer involves dereferencing the pointer then invoking the function.

Invoking a virtual function involves dereferencing the vtable then indexing it for the proper function to be invoked.

This indexing is most likely just a 32/64 bit pointer offset and is probably absolutely tiny. It's about the overhead of indexing an array.

Honestly, you're talking clock cycles here. I am all for being efficient when it matters, and this really isn't going to. Do what is prettiest.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 1:49 pm
by avansc
GyroVorbis wrote:Do what is prettiest.
I agree. I do what is prettiest ;)

Image

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 2:35 pm
by N64vSNES
GyroVorbis wrote:
N64vSNES wrote:Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
That's idiocy. Why don't you go enable RTTI on every project then? Why don't you go enable runtime exceptions? Why are you even writing in C++, if languages like Java and C# have greater overhead and offer more object-oriented power?
Seriously, lets not start another childish war. I never said shit about RTTI. Virtual functions are a basic concept and are prettier than static function pointers. I said "advantage" not "abuse".

avansc wrote:
GyroVorbis wrote:Do what is prettiest.
I agree. I do what is prettiest ;)

Image
Haha :bow:

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 2:58 pm
by Falco Girgis
N64vSNES wrote:
GyroVorbis wrote:
N64vSNES wrote:Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
That's idiocy. Why don't you go enable RTTI on every project then? Why don't you go enable runtime exceptions? Why are you even writing in C++, if languages like Java and C# have greater overhead and offer more object-oriented power?
Seriously, lets not start another childish war. I never said shit about RTTI. Virtual functions are a basic concept and are prettier than static function pointers. I said "advantage" not "abuse".
Look, I'm not trying to randomly flame you. I'm just saying that what you said is stupid.
N64vSNES wrote:I never said shit about RTTI.
N64vSNES wrote:But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
RTTI is an extremely powerful object-oriented mechanism. Java and C# include this by default. It can be extremely useful for a plethora of applications. I have run into many scenarios where saying "fuck it" and removing -fno-rtti would have made my life easier and maybe even my code prettier.

But I didn't do it. Know why? Overhead. C++ is a low-level language most commonly used for performance intensive applications these days (Java and C# have taken over the desktop mainstream). If the philosophy behind C++ were "fuck overhead, lets go all out," RTTI, runtime exceptions, and a few other "additional features" would NOT be disabled by default. I'm sorry, dude, but your statement was stupid, and both the authors of the language and compilers agree with me.

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 4:11 pm
by N64vSNES
GyroVorbis wrote:
N64vSNES wrote:
GyroVorbis wrote:
N64vSNES wrote:Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
That's idiocy. Why don't you go enable RTTI on every project then? Why don't you go enable runtime exceptions? Why are you even writing in C++, if languages like Java and C# have greater overhead and offer more object-oriented power?
Seriously, lets not start another childish war. I never said shit about RTTI. Virtual functions are a basic concept and are prettier than static function pointers. I said "advantage" not "abuse".
Look, I'm not trying to randomly flame you. I'm just saying that what you said is stupid.
N64vSNES wrote:I never said shit about RTTI.
N64vSNES wrote:But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
RTTI is an extremely powerful object-oriented mechanism. Java and C# include this by default. It can be extremely useful for a plethora of applications. I have run into many scenarios where saying "fuck it" and removing -fno-rtti would have made my life easier and maybe even my code prettier.

But I didn't do it. Know why? Overhead. C++ is a low-level language most commonly used for performance intensive applications these days (Java and C# have taken over the desktop mainstream). If the philosophy behind C++ were "fuck overhead, lets go all out," RTTI, runtime exceptions, and a few other "additional features" would NOT be disabled by default. I'm sorry, dude, but your statement was stupid, and both the authors of the language and compilers agree with me.
Dude I gave my own opinion.

1- You're the idiot if you think function pointers are prettier than virtual functions.

2- I'm not saying use it to the max but I think most programs can afford a damn virtual function, quoting what you said "virtual members are kindof the "basis" of object oriented programming, I use the hell out of them"

3- I'm fairly sure we had this argument once on facebook but we seemed to have switched rolls.

I'm going to leave it at that before I get banned ( arguing with Admins arn't my brightest idea's )

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 4:21 pm
by Ginto8
N64vSNES wrote:1- You're the idiot if you think function pointers are prettier than virtual functions.
Remind me who said this?
N64vSNES wrote:2- I'm not saying use it to the max but I think most programs can afford a damn virtual function, quoting what you said "virtual members are kindof the "basis" of object oriented programming, I use the hell out of them"
We're talking about OOP in general. Virtual functions are one of the LEAST overhead inducing OOP features
N64vSNES wrote:3- I'm fairly sure we had this argument once on facebook but we seemed to have switched rolls.
Are you talking about the thing with singletons? Because that was about using OOP correctly, not weighing design against performance.
N64vSNES wrote:I'm going to leave it at that before I get banned ( arguing with Admins arn't my brightest idea's )
yeah that's probably a good idea ;)

Re: which is faster, virtual functions or function pointers?

Posted: Wed Jan 19, 2011 5:13 pm
by Falco Girgis
N64vSNES wrote:
GyroVorbis wrote:
N64vSNES wrote:
GyroVorbis wrote:
N64vSNES wrote:Most people are going to probably be surprised to hear me say this with my current reputation around the forum...

But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
That's idiocy. Why don't you go enable RTTI on every project then? Why don't you go enable runtime exceptions? Why are you even writing in C++, if languages like Java and C# have greater overhead and offer more object-oriented power?
Seriously, lets not start another childish war. I never said shit about RTTI. Virtual functions are a basic concept and are prettier than static function pointers. I said "advantage" not "abuse".
Look, I'm not trying to randomly flame you. I'm just saying that what you said is stupid.
N64vSNES wrote:I never said shit about RTTI.
N64vSNES wrote:But if you're going to write absolutely ANYTHING in C++ then irrelevant of the overhead produced (in my opinion) you should take full advantage of Object Oriented Programming.
RTTI is an extremely powerful object-oriented mechanism. Java and C# include this by default. It can be extremely useful for a plethora of applications. I have run into many scenarios where saying "fuck it" and removing -fno-rtti would have made my life easier and maybe even my code prettier.

But I didn't do it. Know why? Overhead. C++ is a low-level language most commonly used for performance intensive applications these days (Java and C# have taken over the desktop mainstream). If the philosophy behind C++ were "fuck overhead, lets go all out," RTTI, runtime exceptions, and a few other "additional features" would NOT be disabled by default. I'm sorry, dude, but your statement was stupid, and both the authors of the language and compilers agree with me.
Dude I gave my own opinion.

1- You're the idiot if you think function pointers are prettier than virtual functions.

2- I'm not saying use it to the max but I think most programs can afford a damn virtual function, quoting what you said "virtual members are kindof the "basis" of object oriented programming, I use the hell out of them"

3- I'm fairly sure we had this argument once on facebook but we seemed to have switched rolls.

I'm going to leave it at that before I get banned ( arguing with Admins arn't my brightest idea's )
Your responses, your interpretations of the arguments at hand, and your habbit of refuting the small picture when we're talking about the big picture or vice versa REALLY, REALLY makes me think that you have a very low reading comprehension level. I feel like I'm arguing with a child. You would make a terrible attorney.

Looking back at the arguments throughout this post, I don't see a single "retort" from you that was either relevant or correct. You skirt the issues (that you barely understand to begin with), then flip-flop sides and pretend to be a victim, or try to defend yourself from an issue that we aren't even addressing. There is absolutely no point in discussing anything with you, so I correct you as a public service to the forums so that when some poor kid tries to research function pointers or vtables, they don't run into your misinformed bullshit. You think that I'm a dick for calling you on your bullshit? I'm fucking Batman here. Somebody has to be the dark knight for the greater good. Hate me all you want, but at least Gotham is safe, bitch. ;)