Jump to content
  • Advertisement

Archived

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

RedGenie

Structs vs Classes??

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

Does anybody have any ideas on the pros and cons of using Classes against Structs partivularly from a performance point of view. Lets say ive got 100 characters and I increment their position within a loop. The Class way:
  
///////////////////////////

//Character Class (CCharacter)

///////////////////////////

POINT charPosition;

POINT GetPosition() {
  return charPosition;
}

void SetPosition(POINT newPosition) {
  charPosition=newPosition;
}


///////////////////////////

// Main program

///////////////////////////


CCharacter aCharacter[100];

for(int characterCount=0;characterCount<100;characterCount++) {
  int newxPos=aCharacter[characterCount].GetPosition().x+1;
  int newyPos=aCharacter[characterCount].GetPosition().y+1;
  aCharacter[characterCount].SetPosition({newxPos,newyPos});
}
  
And the struct way:
  
///////////////////////////

// Main program

///////////////////////////

struct {
  POINT charPosition;
} CHARACTER;


CHARACTER aCharacter[100];

for(int characterCount=0;characterCount<100;characterCount++) {
  int newxPos=aCharacter[characterCount].charPosition.x+1;
  int newyPos=aCharacter[characterCount].charPosition.y+1;
  aCharacter[characterCount].charPosition={newxPos,newyPos};
}
  
Would the struct way be significantly faster? Probably not for 100 characters, but lets say 2000. This is simplified code, the character class would obviously contain much more functionality but im just wanting to know if there is any significant overhead in having the code class based. (Or is a struct basically a class anyway in which case I guess there would be no difference).

Share this post


Link to post
Share on other sites
Advertisement
Um...I don't know much about OO...but given your example...the struct way should be faster

I'm baseing this on the fact that in the class basied example you are changeing the position by useing a function...which would require some extra work with the stack...pushing/poping variables, jump calls, etc...plus the extra function calls to return the variable values in the class.

In the struct you are changeing them directly (no function calls).

but then I don't know how the compiler deals with classes and such....

Hmm...now that I think about how does the compiler deal with the data in a class?

I meen in the struct you have the bennefit of ajacent memory storage because of the array used...so there is a potential speed increase from useing the cache...and because you are increaseing both X and Y by 1 you could make the routine even faster by dropping the need to temporarily store a integer X and Y value and just use the CPU registers instead...


[edited by - MSW on October 21, 2002 12:37:56 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by RedGenie
Does anybody have any ideas on the pros and cons of using Classes against Structs partivularly from a performance point of view.

Structs are classes in C++, which means that your "struct" example isn''t the C++ use of them, which negates the comparison.

Share this post


Link to post
Share on other sites
You''re thinking too much. It''s not Structs vs Classes. It''s Structs and Classes. Use them where it is appropriate (although c only lets you use structs and c++ rhetoric says ONLY use classes for EVERYTHING). When something needs to be protected or inheritance, polymorphism, etc.., use classes, when something needs quick excessabilty with minimal protection use structs.

"Love all, trust a few. Do wrong to none." - Shakespeare

Dirge - Aurelio Reis
www.CodeFortress.com
Current Causes:
Nissan sues Nissan

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For that specific purpose it is often faster, unless the class''s function is inlined. In an optimised build you are better off profiling it or looking at the assembly output manually if you are picky on performance. I doubt its anything but marginal however.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For that specific purpose the struct is often faster, unless the class''s function is inlined. In an optimised build you are better off profiling it or looking at the assembly output manually if you are picky on performance. I doubt its anything but marginal however.

Share this post


Link to post
Share on other sites

.. from what i've heard functions in a class are automatically inline functions. so the class might run at the same speed (or a little bit faster) as the struct version.

of course i'm a beginner in OO so i'm guessing.
if i'm wrong, they roast (read: flame) me and correct me anyway.

[edited by MKV]



[edited by - MadKeithV on October 22, 2002 7:27:30 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Dirge
c++ rhetoric says ONLY use classes for EVERYTHING



In C++, a struct IS a class, with the ONLY standard difference being that the default access specifier in a struct is public, and in a class private.

It has already been said in this thread too, but apparently, reading is not a requirement for responding anymore.

MSW - there is a potential function call involved with using a function-based modification, yes, but for most short functions the compiler will try to inline it, negating the difference. The keyword 'inline' can be added to demonstrate that you know the function-call overhead might prove a bother, but in the end it's still up to the compiler to decide if it can or cannot inline the function. Usually it does, for short functions like in the example.

[edit: for functions that have their bodies within the class specification (i.e. usually in the header file i.s.o. the CPP file), inline is implied, meaning the compiler will try hard to remove any function-call overhead and some temporary-object construction overhead]



[edited by - MadKeithV on October 22, 2002 5:34:28 AM]

[edited by - MadKeithV on October 22, 2002 8:29:42 AM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!