This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Idea of feature/optimization for C++


Dave, thank you very much for all your comments and also for the google link
(however if you have an exact link to a page that talks about this topic i
would appreciate even more because i found it difficult to find it in google).

Anyway, since you said the optimization is already implemented i have been
trying to test its performance in "gcc version 3.3.3 (mingw special)".
Unfortunatelly the results are not good. I have made a very simple and short
programm in only one short .cpp file. I compiled it in two versions, using
templates and using virtual functions, the performance is pretty different.
The classes are the same HappyHelloWorld and SadHelloWorld of my previous post,
only that this time they dont print anything because i want to measure only the
function calls overhead.
And now instead of calling the functions two times it calls them 200000000 (two
hundred billion times????).
This is the code for the two versions.

main.cpp (virtual function version)
********
#include <iostream>
#include<windows.h>
using namespace std;

class HelloWorld {
    protected:
    HelloWorld() { };
    public:
    // no virtual
    virtual void doIt() = 0; 
};

class HappyHelloWorld : public HelloWorld{
    public:
    HappyHelloWorld() { };
    public:
    virtual void doIt() {};
};

class SadHelloWorld : public HelloWorld {
    public:
    SadHelloWorld() { };
    public: 
    virtual void doIt() {};
};


void letsSee(HelloWorld &hw)
{
    hw.doIt();
}

int main(int argc, char *argv[])
{
  SadHelloWorld shw;
  HappyHelloWorld hhw;
  DWORD previous = GetTickCount();
  for(unsigned i=1; i<100000000;i++) {
     letsSee(hhw);
     letsSee(shw);
  }
   DWORD current = GetTickCount();
   cout << (current-previous) << " milliseconds have passed\n";

  return 0;
}

main.cpp (templates version)
********


#include <iostream>
#include<windows.h>
using namespace std;


class HelloWorld {
    protected:
    HelloWorld() { };
    public:
    // no virtual
    // virtual void doIt() = 0; 
};

class HappyHelloWorld : public HelloWorld{
    public:
    HappyHelloWorld() { };
    public:
    /*virtual*/ void doIt() {};
};

class SadHelloWorld : public HelloWorld {
    public:
    SadHelloWorld() { };
    public: 
    /*virtual*/ void doIt() {};
};

template <class MyHelloWorld>
void letsSee(MyHelloWorld &hw)
{
    hw.doIt();
}

int main(int argc, char *argv[])
{
  SadHelloWorld shw;
  HappyHelloWorld hhw;
  DWORD previous = GetTickCount();
  for(unsigned i=1; i<100000000;i++) {
    letsSee(hhw);
    letsSee(shw);
  }
  DWORD current = GetTickCount();
  cout << (current-previous) << " milliseconds have passed\n";
  return 0;
}


NOW THE RESULTS
***************
I have run each version three times compiling normally and other three times
compiling with -O3.

All the results are in milliseconds.

virtual normal - 4586, 4507, 4546
virtual OPTIMIZED - 1432, 1462, 1452

template normal - 1983, 2043, 2043
template OPTIMIZED - 992. 1022, 1002

My conclusion is that the optimization i am proposing is not implemented in "gcc
version 3.3.3 (mingw special)".

Maybe what you said is implemented is something which i saw documented in some
C++ tutorials, and its optimizing to early binding if you call directly the
virtual memeber function which is of course different of calling a function
which calls the virtual member function. 

Am i right?
Again, I wish i could get some more comments about the optimization i am
proposing.

Quoting Dave Korn <dk@artimi.com>:

> > -----Original Message-----
> > From: gcc-owner  On Behalf Of staube
> > Sent: 01 September 2004 10:30
> 
> > I am writing this mail in order to propose and new 
> > optimization/feature in the
> > C++ front end of GCC.
> 
> > 3) Who knows?, maybe the feature is already implemented, so, 
> > i am not adding
> > anything :-(
> > 
> > The issue i want to cover is virtual functions and 
> > late/early-binding. 
> 
> > As you see both examples do the same thing, only that the 
> > second one uses Early
> > Binding and then is more efficient.
> 
> > functions whenever possible  &#205; was thinking that if the 
> > compiler would be
> > inteligent enought it could do early binding in the first 
> > example too, because
> > theres no need to do late-binding. 
> 
> http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=binding+compile-time+resol
> ution+virtual+function+c%2B%2B
> 
>   It is indeed an excellent idea, and your presentation of the idea and
> analysis of the benefits of this optimisation were spot on, but it has
> already been done; at least to some extent.
> 
> > Another optimization: inline and virtual!!!
> > A method could be declared at the same time inline and 
> > virtual. then when a
> > function call that method using earlybinding it will inline 
> > the function, but
> > in the latebinding version it would call the method in the 
> > standard way.
> 
>   That too is done, in the cases where the compiler can deduce the binding
> at compile time and where the function body has already been seen and was
> defined directly in the class declaration or has the inline attribute
> applied to it.
> 
>     cheers, 
>       DaveK
> -- 
> Can't think of a witty .sigline today....
> 



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]