Class Size
{
Size() : Size(0,0) {}
Size(int x, int y) {...}
SetX ...
SetY ...
}
Class Main
{
private:
Size size;
}
Which way is more efficient?
1)
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
size.SetX(windowWidth);
size.SetY(windowHeight);
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
size = Size(windowWidth, windowHeight)
Alternative?
Assistant
User
I think that the first option is better because I think since the Size size, is defined in the Main, then it internally calls the parameterless constructor, that means the object is already created, so editing the already existing object is better, right? Or it is not created using the parameterless constructor?
Assistant
Your understanding is correct that the Size size member of class Main is created using the parameterless constructor when an instance of Main is instantiated. Here is the corrected code with the correct C++ syntax:
cpp
Your question seems to be about updating a Size object efficiently after retrieving the window size using SDL_GetWindowSize. Between the two options:
Calling SetX and SetY:
cpp
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
size.SetX(windowWidth);
size.SetY(windowHeight);
Assigning a newly constructed Size object to size:
cpp
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
size = Size(windowWidth, windowHeight);
Here is the comparison:
Option 1 updates the existing size object by calling methods SetX and SetY. If SetX and SetY are simple setter methods, then this is fairly efficient assuming the Size object performs minimal work on setting new values.
Option 2 creates a temporary Size object using the constructor, which is then assigned to the size member variable. This approach might involve the temporary object being moved or copied to size depending on whether the assignment operator for Size is defined and whether it can take advantage of move semantics.
From these options, if the methods SetX and SetY are lightweight and the Size class is trivial, updating the members directly via setters (Option 1) might be more efficient or equally efficient while avoiding the construction and assignment of a temporary object.
However, option 2 can be cleaner when it comes to code maintainability because it explicitly shows that both x and y members of size are updated at the same time from one single source (the window size), thus reducing the risk of them being out of sync.
Potential Alternative:
If your Size class allows for it (for example, if you have an appropriate constructor and/or setter that takes both values at once), you could update both values in a single call:
cpp
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
size.SetSize(windowWidth, windowHeight); // Hypothetical method that sets both x and y.
If Size doesn't have this method, you could add it:
cpp
class Size {
public:
Size() : Size(0,0) {}
Size(int x, int y) {/*...*/}
void SetSize(int x, int y) {
// Presumably you'd have a more efficient way to set both at once,
// like directly updating private members, but with validation if needed.
SetX(x);
SetY(y);
}
// Existing SetX...
// Existing SetY...
// ...
};
Another way to handle this with even better efficiency is to modify Size directly without the need for setters, assuming that Size class members are fields that can be set without additional handling:
cpp
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
size.width = windowWidth;
size.height = windowHeight;
Note that the use of public fields (width and height) is sometimes frowned upon because it breaks encapsulation. Yet, in many cases, especially for simple data structures, this can be justified for the sake of efficiency and simplicity.
Overall, choosing the best approach depends on the complexity of your Size class, the cost of construction and assignment of Size objects, and the design trade-offs you're willing to make between efficiency and maintainability.
class Size {
public:
Size() : Size(0,0) {} // Delegating to the parameterized constructor.
Size(int x, int y) { /* ... */ } // Actual initialization.
void SetX(int x) { /* ... */ }
void SetY(int y) { /* ... */ }
// ...
};
class Main {
private:
Size size; // will be initialized by the default constructor Size().
// ...
};
When an instance of Main is created, its size member is indeed initialized with the default constructor Size(). Since size is already constructed, calling its member functions to modify it directly (as in option 1) is typically more efficient than constructing a new Size object and then using assignment to replace the existing size member variable (as in option 2). This is because option 1 is simply modifying the existing object, whereas option 2 creates a temporary object and then uses the assignment operator, which could potentially be less efficient if these operations are complex.
So, between the two original options, the first approach (updating the object via setters SetX and SetY) is often more efficient, as it avoids the overhead of constructing a new Size object and assigning it to the size member. This is especially true if your Size class is non-trivial and the copy/move overhead is significant. It also implies that the setters are lightweight and don't have significant side effects or heavy computations.
Keep in mind that while efficiency is important, the differences here are likely to be small and it is also important to consider the clarity, maintainability, and correctness of the code. In performance-critical code, you should measure the impact of these different approaches if you suspect there might be a noticeable difference. However, in many cases, the compiler optimizations might even eliminate the differences, especially if the Size class is simple or if the compiler can inline the calls to the setters or the constructor.