Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple C++ Inheritance and Performance

Simple C++ Inheritance and Performance

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++oopperformancetutorial
12 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lea Hayes
    wrote on last edited by
    #1

    Hi, I am working on an element of a program which is performance cruical and I wanted to clarify something with regards to basic C++ inheritance. Consider the following: class BaseClass { public: BaseClass(); void foo(); private: int baz; }; class AnotherClass : public BaseClass { public: AnotherClass(); void bar(); }; It is important to note that no virtual functions will be used within this element; including no virtual destructor. My question is this, would there be any performance differences between the above source and the source listed below? class CombinedClass { public: CombinedClass(); void foo(); void bar(); private: int baz; }; The first example would be easier for me to maintain because there will be many variations of 'AnotherClass'. Does anybody know of the nitty-gritty low-level differences between the two and any such differences in performance? Also do overloaded methods cause performance differences? Any advice would be greatly appreciated! Lea Hayes

    F CPalliniC L 4 Replies Last reply
    0
    • L Lea Hayes

      Hi, I am working on an element of a program which is performance cruical and I wanted to clarify something with regards to basic C++ inheritance. Consider the following: class BaseClass { public: BaseClass(); void foo(); private: int baz; }; class AnotherClass : public BaseClass { public: AnotherClass(); void bar(); }; It is important to note that no virtual functions will be used within this element; including no virtual destructor. My question is this, would there be any performance differences between the above source and the source listed below? class CombinedClass { public: CombinedClass(); void foo(); void bar(); private: int baz; }; The first example would be easier for me to maintain because there will be many variations of 'AnotherClass'. Does anybody know of the nitty-gritty low-level differences between the two and any such differences in performance? Also do overloaded methods cause performance differences? Any advice would be greatly appreciated! Lea Hayes

      F Offline
      F Offline
      Florin Crisan
      wrote on last edited by
      #2

      Theoretically there should be no performance difference. The calls to foo() and bar() should be non-virtual in either case. But you can try having a look at the generated assembler code just to be sure. BTW, if it's not a secret, what are you doing so special that you cannot use a virtual function call?

      Florin Crisan

      F L 2 Replies Last reply
      0
      • F Florin Crisan

        Theoretically there should be no performance difference. The calls to foo() and bar() should be non-virtual in either case. But you can try having a look at the generated assembler code just to be sure. BTW, if it's not a secret, what are you doing so special that you cannot use a virtual function call?

        Florin Crisan

        F Offline
        F Offline
        Florin Crisan
        wrote on last edited by
        #3

        On second thought (assuming your constructors are not trivial and cannot be optimized away), if you use two classes you would get two function calls: first to the constructor of the derived class, then to the constructor of the base class. But the other function calls should be the same.

        Florin Crisan

        1 Reply Last reply
        0
        • L Lea Hayes

          Hi, I am working on an element of a program which is performance cruical and I wanted to clarify something with regards to basic C++ inheritance. Consider the following: class BaseClass { public: BaseClass(); void foo(); private: int baz; }; class AnotherClass : public BaseClass { public: AnotherClass(); void bar(); }; It is important to note that no virtual functions will be used within this element; including no virtual destructor. My question is this, would there be any performance differences between the above source and the source listed below? class CombinedClass { public: CombinedClass(); void foo(); void bar(); private: int baz; }; The first example would be easier for me to maintain because there will be many variations of 'AnotherClass'. Does anybody know of the nitty-gritty low-level differences between the two and any such differences in performance? Also do overloaded methods cause performance differences? Any advice would be greatly appreciated! Lea Hayes

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          lhayes00 wrote:

          My question is this, would there be any performance differences between the above source and the source listed below?

          IMHO none noticeable (There are two constructors instead of one, though, if you have a huge number of objects...).

          lhayes00 wrote:

          The first example would be easier for me to maintain because there will be many variations of 'AnotherClass'.

          The above is a good reason to go on with inheritance.

          lhayes00 wrote:

          Also do overloaded methods cause performance differences?

          IMHO no, they are translated by the compiler to different named methods (in fact functions). BTW C++ language is fairly efficient (is one of its design goals), I think that you should first address your efforts to good, clean design and then on optimization of your algorithms, and finally worry about efficiency of different language constructs. Please note that I gave you general guidelines, I'm not an expert of that

          lhayes00 wrote:

          nitty-gritty low-level

          things. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • L Lea Hayes

            Hi, I am working on an element of a program which is performance cruical and I wanted to clarify something with regards to basic C++ inheritance. Consider the following: class BaseClass { public: BaseClass(); void foo(); private: int baz; }; class AnotherClass : public BaseClass { public: AnotherClass(); void bar(); }; It is important to note that no virtual functions will be used within this element; including no virtual destructor. My question is this, would there be any performance differences between the above source and the source listed below? class CombinedClass { public: CombinedClass(); void foo(); void bar(); private: int baz; }; The first example would be easier for me to maintain because there will be many variations of 'AnotherClass'. Does anybody know of the nitty-gritty low-level differences between the two and any such differences in performance? Also do overloaded methods cause performance differences? Any advice would be greatly appreciated! Lea Hayes

            F Offline
            F Offline
            Florin Crisan
            wrote on last edited by
            #5

            I should learn to read the whole message :-O Also do overloaded methods cause performance differences? Well, a normal non-virtual function call has the normal overhead of a, well, normal function call. That is, the arguments are pushed to the stack, the return address is pushed to the stack, etc. (As far as I know) If you have a polymorphic object, in most implementations it keeps a (hidden) pointer to a table of function pointers (known as 'vtable'). So, before making the call, the pointer to the vtable and then the one to the function would have to be de-referenced. (I really don't know how expensive that is; it may be significant on some systems, but probably nothing significant for normal use). However, if the compiler is able to tell the exact type of the object, it won't need to call the function virtually. For example Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure If you really think it would make a difference, try it both with virtual and with non-virtual and see what happens. (And let us known what the result was ;-) )

            Florin Crisan

            F CPalliniC 2 Replies Last reply
            0
            • F Florin Crisan

              I should learn to read the whole message :-O Also do overloaded methods cause performance differences? Well, a normal non-virtual function call has the normal overhead of a, well, normal function call. That is, the arguments are pushed to the stack, the return address is pushed to the stack, etc. (As far as I know) If you have a polymorphic object, in most implementations it keeps a (hidden) pointer to a table of function pointers (known as 'vtable'). So, before making the call, the pointer to the vtable and then the one to the function would have to be de-referenced. (I really don't know how expensive that is; it may be significant on some systems, but probably nothing significant for normal use). However, if the compiler is able to tell the exact type of the object, it won't need to call the function virtually. For example Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure If you really think it would make a difference, try it both with virtual and with non-virtual and see what happens. (And let us known what the result was ;-) )

              Florin Crisan

              F Offline
              F Offline
              Florin Crisan
              wrote on last edited by
              #6

              Googling around for virtual function calls, I found this research paper[^], but I'm too lazy to read it. Hope it helps.

              Florin Crisan

              F 1 Reply Last reply
              0
              • F Florin Crisan

                Googling around for virtual function calls, I found this research paper[^], but I'm too lazy to read it. Hope it helps.

                Florin Crisan

                F Offline
                F Offline
                Florin Crisan
                wrote on last edited by
                #7

                What a mess. It took me so many messages to say so little. :doh: I'm going to get some sleep before the party. Happy New Year!

                Florin Crisan

                1 Reply Last reply
                0
                • F Florin Crisan

                  I should learn to read the whole message :-O Also do overloaded methods cause performance differences? Well, a normal non-virtual function call has the normal overhead of a, well, normal function call. That is, the arguments are pushed to the stack, the return address is pushed to the stack, etc. (As far as I know) If you have a polymorphic object, in most implementations it keeps a (hidden) pointer to a table of function pointers (known as 'vtable'). So, before making the call, the pointer to the vtable and then the one to the function would have to be de-referenced. (I really don't know how expensive that is; it may be significant on some systems, but probably nothing significant for normal use). However, if the compiler is able to tell the exact type of the object, it won't need to call the function virtually. For example Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure If you really think it would make a difference, try it both with virtual and with non-virtual and see what happens. (And let us known what the result was ;-) )

                  Florin Crisan

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Florin Crisan wrote:

                  Also do overloaded methods cause performance differences?

                  Florin Crisan wrote:

                  (As far as I know) If you have a polymorphic object, in most implementations it keeps a (hidden) pointer to a table of function pointers (known as 'vtable'). So, before making the call, the pointer to the vtable and then the one to the function would have to be de-referenced. (I really don't know how expensive that is; it may be significant on some systems, but probably nothing significant for normal use). However, if the compiler is able to tell the exact type of the object, it won't need to call the function virtually. For example Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure

                  Method overload and method override are two quite distinct concepts, he asked for the former, you're talking about the latter.

                  Florin Crisan wrote:

                  Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure

                  AFAIK method invocation is always done via vtables. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  [my articles]

                  In testa che avete, signor di Ceprano?

                  F 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Florin Crisan wrote:

                    Also do overloaded methods cause performance differences?

                    Florin Crisan wrote:

                    (As far as I know) If you have a polymorphic object, in most implementations it keeps a (hidden) pointer to a table of function pointers (known as 'vtable'). So, before making the call, the pointer to the vtable and then the one to the function would have to be de-referenced. (I really don't know how expensive that is; it may be significant on some systems, but probably nothing significant for normal use). However, if the compiler is able to tell the exact type of the object, it won't need to call the function virtually. For example Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure

                    Method overload and method override are two quite distinct concepts, he asked for the former, you're talking about the latter.

                    Florin Crisan wrote:

                    Derived x; x.bar(); // x is a derived -- call Derived::bar() directly Base *x; x->foo(); // is x a Base or a Derived? use the function pointer to be sure

                    AFAIK method invocation is always done via vtables. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    [my articles]

                    F Offline
                    F Offline
                    Florin Crisan
                    wrote on last edited by
                    #9

                    CPallini wrote:

                    Method overload and method override are two quite distinct concepts, he asked for the former, you're talking about the latter.

                    My bad.

                    CPallini wrote:

                    AFAIK method invocation is always done via vtables.

                    I remember reading differently in Stroustrup's book, but it might be just the lack of sleep. I guess it's really up to the compiler's implementer.

                    Florin Crisan

                    CPalliniC 1 Reply Last reply
                    0
                    • F Florin Crisan

                      CPallini wrote:

                      Method overload and method override are two quite distinct concepts, he asked for the former, you're talking about the latter.

                      My bad.

                      CPallini wrote:

                      AFAIK method invocation is always done via vtables.

                      I remember reading differently in Stroustrup's book, but it might be just the lack of sleep. I guess it's really up to the compiler's implementer.

                      Florin Crisan

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Florin Crisan wrote:

                      I remember reading differently in Stroustrup's book

                      Well, I surely don't debate against C++ founder, maybe you're right. Happy New Year :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      [my articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • F Florin Crisan

                        Theoretically there should be no performance difference. The calls to foo() and bar() should be non-virtual in either case. But you can try having a look at the generated assembler code just to be sure. BTW, if it's not a secret, what are you doing so special that you cannot use a virtual function call?

                        Florin Crisan

                        L Offline
                        L Offline
                        Lea Hayes
                        wrote on last edited by
                        #11

                        Hi, It is a math class which will be used extremely frequently. There are a variety of different math classes, and some of which are derived upon the others...There is no multi-inheritance, and I have ruled out virtual methods here because they would offer no gain, and automatically require additional memory to reference a vtable. Thanks for your advice!

                        1 Reply Last reply
                        0
                        • L Lea Hayes

                          Hi, I am working on an element of a program which is performance cruical and I wanted to clarify something with regards to basic C++ inheritance. Consider the following: class BaseClass { public: BaseClass(); void foo(); private: int baz; }; class AnotherClass : public BaseClass { public: AnotherClass(); void bar(); }; It is important to note that no virtual functions will be used within this element; including no virtual destructor. My question is this, would there be any performance differences between the above source and the source listed below? class CombinedClass { public: CombinedClass(); void foo(); void bar(); private: int baz; }; The first example would be easier for me to maintain because there will be many variations of 'AnotherClass'. Does anybody know of the nitty-gritty low-level differences between the two and any such differences in performance? Also do overloaded methods cause performance differences? Any advice would be greatly appreciated! Lea Hayes

                          L Offline
                          L Offline
                          Lea Hayes
                          wrote on last edited by
                          #12

                          Thank you very much for all of your input! I am just about to get started on the link you suggested, it looks for interesting reading. Happy new year!! :-D

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups