Question

Taking this as an example:

I have 20 structs. I access all of the struct's fields directly, getting values off them, pointers to other sub-structs they might have, etc.

Now, I restructure the program: Instead of acessing structs's fields DIRECTLY, I've encapsuled all of the struct into their own respective classes, and have functions for all the possible get(), and set(x).

The Question: Is there a performance impact for using methods/functions, instead of acessing structs directly?

Some sort of estimated % would be great, or some explanation.

Was it helpful?

Solution

In general, there shouldn't be a performance difference if the getters and setters are defined inline within the class. For virtually any contemporary compiler, the function call will be expanded inline, leaving no overhead. This will often be true for various small inline functions. (See, for example, http://www.gotw.ca/publications/mill18.htm, where Herb Sutter discusses why making most/all virtual functions nonpublic adds no overhead to the resulting code.)

Note that if you do define the function inline in the class, any client code will need to be recompiled if the definition ever changes. But that applies to most/all changes in header files.

OTHER TIPS

I haven't heard of performance as a considerable difference between structs and classes, so it is your call. I googled these pages below, and they say there shouldn't be a difference when it comes to performance. http://www.gamedev.net/topic/115725-is-a-struct-faster-than-a-class/ http://forums.codeguru.com/showthread.php?332571-Struct-vs-Class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top