Skip to content

unexpected optimizations #485

@karatse

Description

@karatse

Channel

C++Weekly

Topics

I would love to see some examples where code optimization optimizes "bad" code but not good code.
An example that I've observed with -O3:

void print_int(std::ostream &out, int n) {
    out << "The answer is " << n << ".\n";
}

void print_str(std::ostream &out, int n) {
    out << "The answer is " << std::to_string(n) << ".\n";
}

I would expect the first function to be faster or equivalent to the second one which is not the case, at least on the devices I've tested. The second one is up to 5-10% faster depending the std version.
Do you know other examples like this?

Length

5-10 minutes

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions