Archived

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

poly-gone

Which is more efficient?

Recommended Posts

poly-gone    148
There are 2 ways I can program "stuff" in my engine, the first in a C-like manner (example):- struct Mesh { float pos[3]; ... } void LoadMesh( Mesh* pMesh, char* filename ); and the second in the C++ style (example):- class Mesh { float pos[3]; ... void LoadMesh( ... ); } What I wanna know is which is more efficient, considering memory and speed? Should I stick to the C++ style or is the former a better way to do things?

Share this post


Link to post
Share on other sites
rypyr    252
1) First, When you say "engine", what are you talking about? An actual engine created by someone else or your own hand-rolled set of functions and classes?

2) You might be getting flamed for premature optimization...you should be considering the best design (which, in my opinion is the C++ method) instead of what is more optimal at this stage.

From what I know, when Mesh::LoadMesh() is called, the compiler must first figure out which Mesh object this will apply to. i.e. there is a type of "hidden" parameter to this function. Thus, the two mechanisms are essentially the same thing.

Regards,
Jeff

Share this post


Link to post
Share on other sites
simba    122
There''s really no difference in speed in your two examples. Some will argue that C is faster than C++, and it is in some cases, because there''s less overhead, no dereferencing, etc. But after reading Inside the C++ Object Model by Stanley Lippman, one of the co-authors of the C++ language, I''ve discovered that there is little if any differnce in speed between C and C++, provided you use it right. Thier whole goal was to make sure it was as fast as C. So if you program it correctly, C++ is every bit as fast as C. The problems people have with C++ being slower is typically because of poor programming practices, it''s easy to write really inefficient code, not the language itself.

Share this post


Link to post
Share on other sites
bastardos    100

''tis true. If you are a newb, just learn good design first, then concentrate on speed. However, if you are a newb, it''s probably hard to "see through" all the C++ stuff.. and the compiler would pull some funny trick on you, and you''d end up with damn slow code with no obvious reason.

So, if you''re having really big problems with speed, go with the C style.. otherwise, do it C++ style.

.bas

[sPiKie] mmorpg isnt hard in vb

Share this post


Link to post
Share on other sites
ironfroggy    122
Most the replies have pointed to the C++ version has the better choice, but I wanted to mention why incase any other users are interested.

C version:

struct Mesh
{
float * pos[3];
};
void LoadMesh(struct Mesh * pMesh, char * filename);


C++ version:

class Mesh
{
float * pos[3];
public:
void LoadMesh(char * filename);
};


First of all, rememeber that the only real differences between a struct and a class is the members, by default, are public in a struct and private in a class. Aside from that, there is no difference.

Secondly, remember the only difference between a function and a member-function. That class member-function is getting a second (or rather first) argument, that you don''t see: a pointer to a Mesh object. This is exactly the same as the C version''s function. As a matter of fact, if they were implemented identically, that is, the format of the pos array, you could probably pass the Mesh struct to the member-function of the Mesh class, or pass an instance of the class to the function intended for the struct. As was mentioned by simba, "C++ being slower is typically because of poor programming practices".

Hope that explained everything for anyone who cared to know.

Share this post


Link to post
Share on other sites