Sign in to follow this  

structs vs. classes for geometry data

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

Hi, I would like to know about your experiences concerning the difference in perfomance when using classes instead of structs? I wonder whether I should represent things like vertices, planes and so on through structs and performing operations like dot-product, intersection tests and the like by passing pointer to those structs to static functions of a utility class. How significant do you think would be the perfomance loss/memory consumption when representing the geometry data throug classes with respective member functions instead? In my particular application, there could be tens of thousands of planes for example... Thanks in advance!

Share this post


Link to post
Share on other sites
First of all in c++ there is no inherent speed difference between struct and class, they are exactly the same except for default access method - class is private, struct is public.
The speed difference between calling a global function/static function and a class function is 0 (unless the function is virtual).

Share this post


Link to post
Share on other sites
Quote:
Original post by stevenmarky
The speed difference between calling a global function/static function and a class function is 0 (unless the function is virtual).


I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Share this post


Link to post
Share on other sites
Quote:
I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Correct, so it's not exactly the same!
I.e

class Foo
{
public:
void bar(void)
{
}
};

void s_bar(void)
{
}

void
main()
{
Foo foo;
foo.bar(); // Needs to pass the this pointer to the method, i.e one variable is hidden
s_bar();
}



That said, doing something more practical, like this:


class Foo
{
public:
void bar(void)
{
m_i = 0;
}
int m_i;
};

struct FooStruct
{
int m_i;
};

void s_bar(FooStruct& foo)
{
foo.m_i = 0;
}

void
main()
{
Foo foo;
foo.bar();
FooStruct fooStruct;
s_bar(fooStruct);
}


The above would probably genereate the exact same code for the two calls.

Share this post


Link to post
Share on other sites
Note that the different between "class" and "struct" isn't the same in all languages. Assuming you were talking about C++ then the only different is default access, but in a language like C#, structs are value-types and classes are object-types, which forms a huge difference in performance and usage semantics.

For me, structs typically denote small data structures that exist just as a collection of data, without too many operations. For instance, if I want to pass a collection of particle information to some renderer, where each particle has a bunch of information regarding position, color, velocity, etc., I would use a struct simply because that means to me, "I exist to make storing multiple instances of this data convenient". I would probably use classes for everything else. That's just me though.

Share this post


Link to post
Share on other sites
Quote:
Original post by eq
Quote:
I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Correct, so it's not exactly the same!
I.e
*** Source Snippet Removed ***

That said, doing something more practical, like this:

*** Source Snippet Removed ***
The above would probably genereate the exact same code for the two calls.



Also, in the first example, if the method didn't reference 'this' then the compiler may well optimize it out.

Share this post


Link to post
Share on other sites
Quote:
Original post by xen2
Quote:
Original post by stevenmarky
The speed difference between calling a global function/static function and a class function is 0 (unless the function is virtual).


I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Which, if you're using a global function instead, is replaced by an explicit pointer to the structure. If you didn't need the this pointer, you wouldn't have made it a non-static member function in the first place.

Share this post


Link to post
Share on other sites
The OP needs to answer if they are asking about C++, .NET (probably C#), or another langauge.

In .NET, a struct is a value type and is bit-by-bit copied, potentially using more bandwidth for items more than 8 or so bytes in length ... and also potentially having greater usage speed for items small enough (perhaps less than 16 or even 64 bytes in length). So in general, if you desire value semantics in .NET, but need efficiency, use a value type for items up to 8 bytes, a reference type for items over 64 bytes, and do some profiling for items in between (I usually use value up to 16-20 bytes).

In C++, struct and class are irrelevant completely.

In C++ the choice is member functions, global functions with "this" equivelent" and global functions with no need for a "this" like pointer. The first 2 are identical if not virtual ... and the later is "faster" because in this case the algorithm obviously has no need for member data, and therefore shouldn't be a "member" .. therefore it should be "static"

Share this post


Link to post
Share on other sites
If we are not talking about virtual member functions (I think this is not the case) there is no loss in terms of memory or performance.

I suggest you to take a look at the Unreal Public Source Code (sorry this is the first link I found) to see how a "real" math engine can be written... years ago it was usefull for me! See core/unmath.h for a mine of tips!

If you are concerned about speed, the real optimization is to implement expensive operations (for example matrix products) via assembly. But the container should be in pure Cpp if you intend to use this language.

Share this post


Link to post
Share on other sites

This topic is 4022 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this