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

  • Home
  • SEARCH
  • 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 7653335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:00:28+00:00 2026-05-31T12:00:28+00:00

While playing with VS11 beta I noticed something weird: this code couts f took

  • 0

While playing with VS11 beta I noticed something weird:
this code couts

f took 0 milliseconds

int main()
{
    std::vector<int> v;
    size_t length =64*1024*1024;
    for (int i = 0; i < length; i++)
    {
        v.push_back(rand());
    }

    uint64_t sum=0;
    auto t1 = std::chrono::system_clock::now();
    for (size_t i=0;i<v.size();++i)
        sum+=v[i];
    //std::cout << sum << std::endl;
    auto t2 = std::chrono::system_clock::now();
    std::cout << "f() took "
        << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
              << " milliseconds\n";


}

But when I decide to uncomment the line with couting of the sum then it prints out a reasonable number.

This is the behaviour I get with optimizations enabled, with them disabled I get “normal” cout

f() took 471 milliseconds

So is this standard compliant behaviour?
Important: it is not that dead code gets optimized away, I can see the lag when running from console, and I can see CPU spike in Task Manager.

  • 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-05-31T12:00:29+00:00Added an answer on May 31, 2026 at 12:00 pm

    My guess is that this is dead code optimization – and that your load spike is due to the work initializing the vector isn’t being optimized away, but the computation of your unused sum variable is.

    But when I decide to uncomment the line with couting of the sum then it prints out a reasonable number.

    That goes along with my theory, yes – when you’re forced to use the result of the computation, the computation itself can’t be optimized away.

    If you want to confirm that further, make your program say when it’s ready and pause for you to press return – that will allow you to wait for any CPU spike to be obviously “gone” before you press return, which will give you more confidence about what’s causing it.

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

Sidebar

Related Questions

While stepping through code today, I noticed something unexpected. This statement: if Object.respond_to? :hello
While playing around with regexps in Scala I wrote something like this: scala> val
I stumbled upon this oddity today while playing with some code to go down
I have been playing with this for a while, but the closest I have
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
While playing around with the emulator, I noticed that when trying to view a
while playing to this game I wondered how an AI controlling either the detectives
I've seen a few function definitions like this recently while playing with GNU Bison:
I was working on a project and, while playing around with the code, I
While playing around with some hooks I noticed that the output from the hooks

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.