structs and classes...blah blah

Started by
15 comments, last by quackface 19 years, 10 months ago
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!!!!
Advertisement
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]
I''ve done both these things.
It makes no difference.
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?
[size="1"]
Using .. compiler?

--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net | GameDev.net''s DirectX FAQ. (not as cool as the Graphics and Theory FAQ)
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.
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]
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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]
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.

This topic is closed to new replies.

Advertisement