Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comparison with short (< 16 chars) local const std::string is slower when it is static #60165

Open
firewave opened this issue Jan 20, 2023 · 1 comment

Comments

@firewave
Copy link

Split from #58002.

#include <string>

#define LITERAL_S "012345678901234"
#define LITERAL_L "0123456789012345"

bool cmp3_s(const std::string& s)
{
    const std::string s_s(LITERAL_S);
    return s == s_s;
}

bool cmp4_s(const std::string& s)
{
    static const std::string s_s(LITERAL_S);
    return s == s_s;
}

bool cmp3_l(const std::string& s)
{
    const std::string s_s(LITERAL_L);
    return s == s_s;
}

bool cmp4_l(const std::string& s)
{
    static const std::string s_s(LITERAL_L);
    return s == s_s;
}

https://quick-bench.com/q/A3FPfNV6Cds2TwwBvSRwT6klxLg

This applies to all strings with less than 16 characters.

This does not happen with GCC where static is always faster no matter the length:
https://quick-bench.com/q/hPor-y8JDwcMBEnT_oOv79nF43g
https://quick-bench.com/q/4Ef2dOfaBuhCd0QkKBRVVCXamjI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants