Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BSXrider

"private:" data members SLOOOW

This topic is 6017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I always compile on Release. MSVC++6. Optimisations set to "maximum" speed.

What do you think is crappy about my compiler?

- seb

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!