Question

Code:

namespace Dialog {
    enum class Type {Info, Warning, Error};
    enum class Buttons {OK, OK_CANCEL, YES_NO, YES_NO_CANCEL};
    void Box(Pane* parent, Core::String, Core::String, Buttons, Type);
}

I could define InfoBox like this:

void InfoBox(Core::String title, Core::String text, Buttons b) {
    Box(nullptr, title, text, b, Type::Info);
}

but with this approach I have overhead due to the extra function call.

How can I reduce the overhead?

Was it helpful?

Solution

There won't be any. The compiler will trivially inline your function. Even if it didn't, the overhead of the call will add up to absolutely nothing compared to other GUI overheads.

OTHER TIPS

For the sake of argument, let's say a CPU can execute one instruction per cycle.

Let's say you're running on an old, slow CPU which runs at a mere 1GHZ. And let's assume it only has a single core.

A function call consists basically of pushing a few values onto the stack, jumping to another location, popping a few values off the stack, and, once the function is completely, pushing the return value onto the stack (under some calling conventions), and jumping back to where the function was called from.

Let's call it 10 instructions overall. Let's say your user is completely and utterly insane, and he opens 1000 InfoBoxes every second.

That means you waste 1000 * 10 instructions = 10,000 instructions per second.

The CPU can run 1,000,000,000 instructions every second. That means you're wasting 0.0001% of the available CPU time.

Is that something to worry about?

And this is without taking compiler optimizations into account, which would probably reduce the overhead to, well, a big fat zero. (The function call is very likely to be inlined). But in the worst case, a rough approximation is that you're wasting one thousandth of one percent of your CPU's execution time in the worst case, if the user is unrealistically eager about displaying InfoBoxes.

If I am understanding you correct,you can use the concept of default argument

void InfoBox(Core::String title="Hello", Core::String text="Info", Buttons b=Buttons::OK,Type typ=Type::Info) {
    Box(nullptr, title, text, b, Type::Info);
}

Or Also,

void Box(Pane* parent=0, Core::String title="Hello", Core::String text="Info", Buttons b=Buttons::OK,Type typ=Type::Info);

And then your calls will range from

InfoBox();
InfoBox("Hi");
InfoBox("Hi","Welcome");
InfoBox("Hi","Welcome",b=Buttons::OK_CANCEL);
InfoBox("Hi","Welcome",b=Buttons::OK_CANCEL,Type::Warning);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top