Archived

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

quackface

structs and classes...blah blah

Recommended Posts

quackface    122
I was reading through some posts on the old "struct v class" debate. I always suspected structs where faster than classes in certain circumstances, so i did an experiment: I made two data structures: struct vector { float v[3]; }; class vector { public: float v[3]; vector(); }; then i ran them through a test program which simple declared a bunch of them, and i found that using structs was over 275% faster than classes. where is this horrendous constructor overhead coming from? can this be optimised? apologies for bringing back this debate!!!!

Share this post


Link to post
Share on other sites
Gorg    248
That's not the same thing.

You struct constructor is probably being inlined.
So write a constructor just like you did for your class and try again. Or remove the constructor from the class and try again.




[edited by - Gorg on May 26, 2004 10:45:21 AM]

Share this post


Link to post
Share on other sites
mrbastard    1577
weird! What compiler are you using BTW?

Have you tried the same thing but with them both having a few member functions, maybe try some inheritance?

Share this post


Link to post
Share on other sites
quackface    122
I''m using MSVC++ 6.
It is odd. If you put a constructor in the struct it takes the same time to instantiate as the class. But if you take the constructor out of the class, the time to instatiate that class stay the same (i.e. ages!).
This is slightly worrying for something as heavily used as a vector.

I''ll do some more experiements.

Share this post


Link to post
Share on other sites
BiTwhise    134
the only difference between structs and classes in C++ is that class members default to private, while struct members default to public.. so these results are strange indeed :/

in .net managed langauges, like C#, however, structs are valuetypes and as such not allocated as objects (but rather on the stack, or in heap allocated arrays (not by reference) )

-- EDIT --
tried keeping the ctor in the class, but using the ms specific "__inline" declarator?

[edited by - BiTwhise on May 26, 2004 11:10:55 AM]

Share this post


Link to post
Share on other sites
Agony    3452
I''m gettin'' equal times for both classes and structs. A class with a constructor much slower than one without, and the same for a struct, but a class with a constructor is equal to a struct with a constructor, and a class without is equal to a struct without.

This is MSVC6 as well. It''s in debug mode, ''cause I can''t get the compiler to quit removing the entire loop in release mode. Times of 0.000000 seconds are kinda worthless for comparison.

Share this post


Link to post
Share on other sites
quackface    122
i'm gonna investigate this when i get back from work tonight.
you just know it's gonna be something really stupid i've done wrong.
always the way (where's the valium!)

it doens't help when i'm switching between vb, asp, php and javascript in the space of a few hours (damn you projects!!)

[edited by - quackface on May 26, 2004 11:37:36 AM]

Share this post


Link to post
Share on other sites
quorn    145
There is no difference.

Make sure you''re not using debug mode.

a struct is a class with public members by default which inherits publicly by default.

a class has private members by default and inherits privately by default

and that''s it.

whether things are public or private doesn''t affect the speed of the compiled code. they stop you making silly mistakes but the compile can optimise as much as it likes.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites