Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7943571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:18:34+00:00 2026-06-04T00:18:34+00:00

Do C and C++ compilers generally optimize comparisons with functions? For example, this page

  • 0

Do C and C++ compilers generally optimize comparisons with functions?

For example, this page suggests that the size function on std::lists in C++ can have a linear complexity O(N) in some standard library implementations (which makes sense for a linked list).

But in that case, if myList is a huge list, what would something like this do?

    if (myList.size() < 5) return 1;
    else return 2;

Would the size() function find and count all N list members, or would it be optimized to short circuit after finding 5 members?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:18:35+00:00Added an answer on June 4, 2026 at 12:18 am

    Theoretically the possibility exists if size() was inlined, but to perform the optimization the compiler would have to

    1. Detect that you are testing specifically a “less than” condition
    2. Prove that the loop (assume one exists for the purposes of this discussion) results in a variable increasing monotonically
    3. Prove that there are no observable side effects from the loop body

    That’s a big bunch of things to count on IMHO, and it includes features which are not “obviously useful” in other contexts as well. Keep in mind that compiler vendors have limited resources so there has to be really good justification for implementing these prerequisites and having the compiler bring all the parts together to optimize this case.

    Seeing as even if this is a perf issue for someone the problem can be easily solved in code, I don’t feel that there would be such justification. So no, generally you should not expect cases like this to be optimized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Compilers can sometime exploit the fact that some 'variable' is a constant for optimization,
Generally I need some online compiler that can compile and execute provided program and
I'm using a logging module that can have reporting enabled/disabled at runtime. Calls generally
If I have a function that returns an object, but this return value is
How do you optimize the heap size usage of an application that has a
I've learned that Xor operation can be used to implement effective swap function. like
How ASP.NET compiles its assemblies generally confuses me. It seems that I cannot program
Using the C# compilers query comprehension features, you can write code like: var names
Possible Duplicate: Plugging in to Java compilers Edit - this appears to be a
I read that early C++ compilers actually translated the C++ code to C and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.