structs and classes...blah blah

Started by
15 comments, last by quackface 19 years, 10 months ago
Well i did the same test using Visual C++ 2003.net pro and the Devpartner community edition profiler, here is the output from the profiler.

							struct structvector				{				float v[3];				};								class classvector				{				public:				float v[3];				classvector(){}				};								int main()	1	0.0	0.9	{	1	0.0	0.2	    for(unsigned int count = 0; count < 20000; ++count)				    {				        classvector v;	20,000	46.6	3,045.5	        v.v[0]=1;				        structvector k;	20,000	47.9	3,124.8	        k.v[0]=1;				    }	1	0.0	0.4	}	


The numbers in the order that they appear are Count, % of program spent in children, execution time spent in children. As you can see, the constructors didnt register in the code, the % of execution time spent accessing the array in the class and struct was roughly the same, with the class version slightly faster.

[edited by - Jingo on May 26, 2004 11:46:29 AM]
Advertisement
do you know what guys?
i think i deserve a right royal slap on the bottom!

the problem i think is the constructor
vector();
should be vector(){}

as in the above post.

am i a goon or what?

oh well, i think that''s possibly cleared things up for me.

cheers people!!!!!!!
so, it looks like if the msvc6 compiler detects theres no constructor, it assumes it's a c-sytle struct (eg just a collection of variables) and removes the initialisation overhead. Fair enough really.

EDIT: OK, it doesn't. ;¬)

[edited by - mrbastard on May 26, 2004 11:51:04 AM]
[size="1"]
It as already been mentionned. Inline your constructor.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
the curly braces:
vector(){}

make it inline removing the overhead

what a plonker i really am. indeed.

you know what it''s like, you get locked into something and you can''t see what''s staring you in the face.



stay stress free guys!

The point is: function definitions inside the class definition are always inline.

Don't confuse a function definition with a function declaration.


--
You're Welcome,
Rick Wong
- Google | Google for GameDev.net | GameDev.net's DirectX FAQ. (not as cool as the Graphics and Theory FAQ)

[edited by - Pipo DeClown on May 26, 2004 12:39:59 PM]
Although you''ve already solved this mystery, here''s another suggestion the next time you profile something: always compile it in release mode! If it gets rid of the loop, then put something in there so it won''t just remove it.

This topic is closed to new replies.

Advertisement