"private:" data members SLOOOW

Started by
21 comments, last by BSXrider 22 years, 3 months ago
Benchmarking: 1) code as below in source: 17.3 seconds 2) as below but "void" setx member function with no return: 17.5 seconds (!) 3) as below but passing i by value instead of reference in setx: 19.0 seconds 4) using "public:" data members and "v1.x" instead of set/get routines: 3.8 seconds. Can things be speeded up without the cretinous sin of public data members? edit: 5) solved using "inlines". 3.9 seconds. - seb
    
float Vect::getx() const
{
	return xcoord;
}

Vect &Vect::setx(const float & value)
{
	xcoord = value;

	return *this;
}

int main()
{
	int i=0;
	float x=0;

	Vect v1(0,0,0);
	
	long time0=GetTickCount();
	
	for(;i<1000000000;++i)
	{
		v1.setx(i);
		x=v1.getx();
	}

	long time1=GetTickCount();
	
	cout << time1-time0;

	return 0;
}
    
Edited by - bsxrider on January 29, 2002 7:53:53 AM
Advertisement
You should put your accessor member functions in the header of your class and declare them as 'inline'.

Here's a better implementation of your class :

    class Vect{private:   float   xcoord;public:   inline float getx() const   {      return xcoord;   }   inline void setx( float value)   {      xcoord = value;   }};     


This way, accesses to xcoord should be as fast as if it were accessed directly.

Note : you don't need to use references to pass const standard type arguments (int/float/char/...), you won't have any speed increase for this.

Edited by - Prosper/LOADED on January 29, 2002 7:44:25 AM
well, I'd use public variables for a vector class, but if you are dead set on keeping them private you can always declare the functions inline.

eg
      inline float Vect::GetX(void)const ;      



[edit]

too slow


Edited by - Sandman on January 29, 2002 7:49:11 AM
Ahhh. I tried to "inline" my functions as another test but it was coming up as errors, didn''t realise thery ahd to go with the definitions.

3.9 seconds.

Good stuff

- seb
quote:
you don''t need to use references to pass const standard type arguments (int/float/char/...), you won''t have any speed increase for this.


Yeah I know, but my experiment in the first post shows that reference actually WAS slightly faster. However, with the tuples inlined the time is EXACTLY the same either way.

- seb

Either you have a REALLY crappy compiler, or you are compiling a debug version (or both ).

If you want to do ANY kind of benchmarking, you should compiler a release version, preferably with full optimization. Benchmarking on debug code is useless, and does not say/mean anything.
I always compile on Release. MSVC++6. Optimisations set to "maximum" speed.

What do you think is crappy about my compiler?

- seb
If you do compile a realease, then I apologize. And I use the MSVC 6 compiler myself, and it''s nowhere near crappy

Anywyas, if you don''t want to make your members public, then inlined functions is probbaly the fastest. Just try them and choose the fastest one.
i find that very wierd too. vc++ should be inlining those functions due to their size.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Just loaded up the options bit.

Under "Optimizations" there''s an option called "Inline function expansion". Don''t know what it means really but I changed it to "any suitable (from __inline) and it made no difference.

If I put the member functions inside the class definition then they''re automatically inlined, it just seems to be if you declare it in the class and have the function in a separate file that it doesn''t automaticvally inline them. Interestingly in this case it would let you speciify that they''re inlined either.

- seb

This topic is closed to new replies.

Advertisement