https://en.cppreference.com/w/cpp/language/attributes/likely cppreference.com [ ] [Search] Create account * Log in Namespaces * Page * Discussion Variants Views * View * Edit * History Actions C++ attribute: likely, unlikely (since C++20) From cppreference.com < cpp | language | attributes C++ Compiler support Freestanding and hosted Language Standard library Standard library headers Named requirements Feature test macros (C++20) Language support library Concepts library (C++20) Metaprogramming library (C++11) Diagnostics library General utilities library Strings library Containers library Iterators library Ranges library (C++20) Algorithms library Numerics library Localizations library Input/output library Filesystem library (C++17) Regular expressions library (C++11) Concurrency support library (C++11) Technical specifications Symbols index External libraries [edit] C++ language General topics Preprocessor Keywords Comments Escape sequences Flow control Conditional execution statements if switch Iteration statements (loops) for while range-for (C++11) do-while Jump statements continue - goto - return break Functions Function declaration Lambda function expression inline specifier Dynamic exception specifications (until C++17*) noexcept specifier (C++11) Exceptions throw-expression try-catch block Namespaces Namespace declaration Namespace aliases Types Fundamental types Class/struct types Enumeration types Union types Function types Specifiers decltype (C++11) const/volatile auto (C++11) constexpr (C++11) alignas (C++11) Storage duration specifiers Initialization Default-initialization Aggregate initialization Value-initialization List-initialization (C++11) Zero-initialization Constant initialization Copy-initialization Reference initialization Direct-initialization Expressions Value categories Operators Order of evaluation Operator precedence Alternative representations Literals Boolean - Integer - Floating-point Character - String - nullptr (C++11) User-defined (C++11) Utilities Attributes (C++11) Types typedef declaration Type alias declaration (C++11) Casts Implicit conversions - Explicit conversions static_cast - dynamic_cast const_cast - reinterpret_cast Memory allocation new expression delete expression Classes Class declaration Access specifiers Constructors friend specifier this pointer Class-specific function properties Virtual function explicit override specifier (C++11) (C++11) final specifier (C++11) static Special member functions Default constructor Copy assignment Copy constructor Move assignment (C++11) Move constructor (C++11) Destructor Templates Class template Template specialization Function template Parameter packs (C++11) Miscellaneous Inline History of C++ assembly [edit] Declarations Overview declaration syntax decl-specifier-seq declarator Declarators Specifiers reference typedef pointer inline array virtual function specifier Block declarations explicit function specifier simple-declaration - structured binding declaration friend (C++17) constexpr alias declaration(C++11) (C++11) namespace alias definition consteval using-declaration (C++20) using-directive constinit static_assert declaration (C++11) (C++20) asm-declaration storage class specifiers opaque enum declaration(C++11) translation-unit-local Other declarations (C++20) namespace definition class/struct function declaration union class template declaration enum function template declaration decltype explicit template instantiation(C++11) (C++11) explicit template specialization auto linkage specification (C++11) attribute declaration (C++11) alignas empty declaration (C++11) constvolatile elaborated type specifier attributes (C++11) [edit] Attributes noreturn (C++11) carries_dependency (C++11) deprecated (C++14) fallthrough (C++17) nodiscard (C++17) maybe_unused (C++17) likelyunlikely (C++20)(C++20) no_unique_address (C++20) assume (C++23) optimize_for_synchronized (TM TS) [edit] Allow the compiler to optimize for the case where paths of execution including that statement are more or less likely than any alternative path of execution that does not include such a statement. [edit] Syntax [[likely]] (1) [[unlikely]] (2) [edit] Explanation These attributes may be applied to labels and statements (other than declaration-statements). They may not be simultaneously applied to the same label or statement. 1) Applies to a statement to allow the compiler to optimize for the case where paths of execution including that statement are more likely than any alternative path of execution that does not include such a statement. 2) Applies to a statement to allow the compiler to optimize for the case where paths of execution including that statement are less likely than any alternative path of execution that does not include such a statement. A path of execution is deemed to include a label if and only if it contains a jump to that label: int f(int i) { switch (i) { case 1: [[fallthrough]]; [[likely]] case 2: return 1; } return 2; } i == 2 is considered more likely than any other value of i, but the [[likely]] has no effect on the i == 1 case even though it falls through the case 2: label. [edit] Example Run this code #include #include #include #include #include namespace with_attributes { constexpr double pow(double x, long long n) noexcept { if (n > 0) [[likely]] return x * pow(x, n - 1); else [[unlikely]] return 1; } constexpr long long fact(long long n) noexcept { if (n > 1) [[likely]] return n * fact(n - 1); else [[unlikely]] return 1; } constexpr double cos(double x) noexcept { constexpr long long precision{16LL}; double y{}; for (auto n{0LL}; n < precision; n += 2LL) [[likely]] y += pow(x, n) / (n & 2LL ? -fact(n) : fact(n)); return y; } } // namespace with_attributes namespace no_attributes { constexpr double pow(double x, long long n) noexcept { if (n > 0) return x * pow(x, n - 1); else return 1; } constexpr long long fact(long long n) noexcept { if (n > 1) return n * fact(n - 1); else return 1; } constexpr double cos(double x) noexcept { constexpr long long precision{16LL}; double y{}; for (auto n{0LL}; n < precision; n += 2LL) y += pow(x, n) / (n & 2LL ? -fact(n) : fact(n)); return y; } } // namespace no_attributes double gen_random() noexcept { static std::random_device rd; static std::mt19937 gen(rd()); static std::uniform_real_distribution dis(-1.0, 1.0); return dis(gen); } volatile double sink{}; // ensures a side effect int main() { for (const auto x : {0.125, 0.25, 0.5, 1. / (1 << 26)}) std::cout << std::setprecision(53) << "x = " << x << '\n' << std::cos(x) << '\n' << with_attributes::cos(x) << '\n' << (std::cos(x) == with_attributes::cos(x) ? "equal" : "differ") << '\n'; auto benchmark = [](auto fun, auto rem) { const auto start = std::chrono::high_resolution_clock::now(); for (auto size{1ULL}; size != 10'000'000ULL; ++size) sink = fun(gen_random()); const std::chrono::duration diff = std::chrono::high_resolution_clock::now() - start; std::cout << "Time: " << std::fixed << std::setprecision(6) << diff.count() << " sec " << rem << std::endl; }; benchmark(with_attributes::cos, "(with attributes)"); benchmark(no_attributes::cos, "(without attributes)"); benchmark([](double t) { return std::cos(t); }, "(std::cos)"); } Possible output: x = 0.125 0.99219766722932900560039115589461289346218109130859375 0.99219766722932900560039115589461289346218109130859375 equal x = 0.25 0.96891242171064473343022882545483298599720001220703125 0.96891242171064473343022882545483298599720001220703125 equal x = 0.5 0.8775825618903727587394314468838274478912353515625 0.8775825618903727587394314468838274478912353515625 equal x = 1.490116119384765625e-08 0.99999999999999988897769753748434595763683319091796875 0.99999999999999988897769753748434595763683319091796875 equal Time: 0.579122 sec (with attributes) Time: 0.722553 sec (without attributes) Time: 0.425963 sec (std::cos) Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp /language/attributes/likely&oldid=151939" Navigation * Support us * Recent changes * FAQ * Offline version Toolbox * What links here * Related changes * Upload file * Special pages * Printable version * Permanent link * Page information * In other languages * Espanol * Ri Ben Yu * Zhong Wen * This page was last modified on 20 May 2023, at 08:15. * This page has been accessed 180,184 times. * Privacy policy * About cppreference.com * Disclaimers * Powered by MediaWiki Powered by GeSHi Hosted by Tiger Technologies