Jump to content
  • Advertisement

Archived

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

quackface

structs and classes...blah blah

This topic is 5112 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

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
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]

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!