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 8566419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:39:02+00:00 2026-06-11T17:39:02+00:00

Full disclosure – I was inspired by Is x += a quicker than x

  • 0

Full disclosure – I was inspired by Is x += a quicker than x = x + a?

That aside, I decided to test += vs -=. Simple tests reveal they’re about the same. Then I tried something similar to:

std::vector<int> x;
for (int i = 0 ; i < 10000 ; i++)
   x.push_back(rand()%10);

and call += and -= proportionally to a given number:

long long sum = 0;

for ( each number in the array )
    if ( x[j] < k )
        sum += x[j];
    else
        sum -= x[j];

so, if k is, say, small, -= would get called more often (duuuh). I tried with k = 2 which would give a higher proportion of -= called, and with k = 5, which should yield about the same number of -= and +=.

The punchline: calling -= is about twice as faster than calling +=. Why would it be more efficient in this case?

  • 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-11T17:39:03+00:00Added an answer on June 11, 2026 at 5:39 pm

    I’m gonna jump in before Mysticial gets a hold of this and guess: branch prediction.

    So, it’s not the -= vs +=.

    The condition x[j] < k can be better predicted when it’s almost always true or false than it can be when there’s about the same number of numbers for which it can evaluate to either.

    For k = 2, one in 10 will evaluate to false.

    For k = 5, they’ll be about the same and distributed randomly, so harder to predict.

    EDIT: See http://ideone.com/1PYMl – all extra stuff is there to prevent unused code optimization (the couts).

    tl;dr: Results for varying k:

    k: 1 Time: 280
    k: 2 Time: 360
    k: 3 Time: 440
    k: 4 Time: 520
    k: 5 Time: 550
    k: 6 Time: 510
    k: 7 Time: 450
    k: 8 Time: 360
    k: 9 Time: 260
    

    as you can see, the closer k gets to a chaotically varying condition, the program takes more. Towards the ends, it takes about half the time.

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

Sidebar

Related Questions

Full disclosure, I'm new to Visual Studio Web Tests and coding for them. I've
First, in order to provide full disclosure, I want to point out that this
Full Disclosure: I am bad at javascript. I'm trying to write something that takes
Full disclosure: this is for a school project. The project is in C++. They
Full disclosure: this is for an assignment, so please don't post actual code solutions!
Full disclosure : This is for a homework assignment. This is driving me nuts.
The full message that I got on the console is: Invalid memory access of
First off, full disclosure: This is going towards a uni assignment, so I don't
Full edit: The scenario is that after uploading the file to the server via
Full Tilt Poker's GUI has a box of text that keeps reporting on the

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.