C++ (premature) optimization
17 minutes ago, fpo said:Integer types use 2 bytes of data. Pointers store an address in 1 byte of data (I think) and that's what makes them more efficient.
In a 64 bit application, a pointer takes up 64 bits = 8 bytes (and on 32 bit, it's 4 bytes). The size of an int varies by platform, but is generally 32 bits = 4 bytes.
Optimising a couple of bytes is very definitely premature optimisation. However, in this case method 1 will be more efficient.
In terms of memory, method 1 has 4 bytes for x, while method 2 has 8 bytes.
In terms of speed, method 2 involves a pointer dereference, which is a minimum of one extra instruction (and in the event of a cache miss, it will take several cycles).
25 minutes ago, fpo said:I'm working with SFML & want to create many of the same object with the same dimensions so I created a variable elsewhere to make it easier to change the sizes if I need to.
For that use case, it's usually a better idea to use preprocessor macros, which are resolved at compile time and have no runtime overhead
#define CUSTOM_C_DIMENSIONS 10
Then rather than accessing CustomC::x, you would just use CUSTOM_C_DIMENSIONS.
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 accountSign in
Already have an account? Sign in here.
Sign In Now