[solved] virtual functions or function pointers?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Void Mapper
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Jan 04, 2011 11:00 pm
Programming Language of Choice: C++

[solved] virtual functions or function pointers?

Post 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)?
Last edited by Void Mapper on Wed Jan 19, 2011 3:51 pm, edited 1 time in total.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post 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...
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

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

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Void Mapper
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Jan 04, 2011 11:00 pm
Programming Language of Choice: C++

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

Post 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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

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

Post 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?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post by avansc »

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

Image
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

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

Post 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:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

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

Post 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 )
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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 ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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. ;)
Post Reply