[quote name='Hodgman' timestamp='1345010440' post='4969749']The point of mentally translating your C++ code to C code is to be aware of the actual meaning of what you're writing. If you can't carry out this exercise, then you don't really understand C++ and are a danger to your project.
This was a fairly strong statement, so I decided to test myself and take my previous example of a virtual function, and translate it over to C. I then took the C implementation and "optimized" it by folding the vtable into the object (
seeing that there was only one function in the vtable, this should be a space saving), which, in theory, reduces the complexity of calling the "virtual" function.
I tested each implementation by calling the virtual function on an object of the derived type (
where the compiler should be able to resolve the polymorphism at compile time) and also through a pointer to the bass class (
so dynamic-dispatch must be performed).
I was very surprised by the results. All 3 performed the exactly same when calling the function on the derived type (
I was kinda suprised that the C++ compiler optimised out the 'virtual' overhead in this case!), but when calling the function via the base, things got weird. The straightforward C++ version won outright. The C translation was almost the same, but the compiler didn't optimise it as well, so it was slightly slower. The optimised C++ version produced much simpler assembly as predicted, but for some reason was slower! I've yet to figure out why, but I'm pretty intrigued.
Here's my test:
http://pastebin.com/X2s5Nne6tl;dr: I set up a C++ straw-man to show how reliance on C++'s features can be slow... but proved the opposite?
![blink.png](http://public.gamedev.net//public/style_emoticons/default/blink.png)
Or I just proved that optimisations aren't intuitive?
![huh.png](http://public.gamedev.net//public/style_emoticons/default/huh.png)
Or that I'm a bad programmer and a danger to my project?
![wink.png](http://public.gamedev.net//public/style_emoticons/default/wink.png)
[/quote]
Bad programmer? Not at all, you have at least found an issue and know how to look for it, and fix it hopefully, I would say that makes a decent programmer at the very least!
![:)](http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/smile.png)
Bad would have been assuming it was the best, and having no idea how to fix it, or if it was even an issue... IMO