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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:38:06+00:00 2026-06-11T12:38:06+00:00

As soon as you declare a variable you start to manipulate it. My question

  • 0

As soon as you declare a variable you start to manipulate it. My question is on the speed of manipulation, if it even matters.
Lets say you have a variable total, which is the sum of all values in an array. You want to find the average so you will do total / length. Is it faster for you to declare a new value (ie double avg = total / length), or is it faster to do everything “in place” ( total \= length ), this would of course by ignoring the fact that total is a poor variable name for average, but this is simply to get my point across

I suspect as the processor needs to copy the data from RAM to do the calculation in the ALU or even hardwired calculation in the CPU via bit shifting, but I am not really sure how C++ handles memory, and I am curious as to how that works on the back end.

  • 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-11T12:38:07+00:00Added an answer on June 11, 2026 at 12:38 pm

    It depends on how often you will use the average. If it is only used a few times then it’s negligible, but if you need to use that average hundreds or thousands of times then it may be “faster” to use a variable as you don’t have to do the calculation all the time. However, the compilers today are very good when it comes to optimizations, so in reality it’s probably not going to matter.

    The only to know for sure is to test it, do some profiling and measurements. Something you should always do before thinking about manual optimizations (I doubt calculating averages, or even more complex things, is going to be a bottleneck.)

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

Sidebar

Related Questions

We'll soon have some high school kids here in our company for some training
I have a process which is repeated on a set of data stored in
Referring on this question , I have a similar -but not the same- problem..
This is my first question, and something which has got me stumped. I'm not
I have a table in Sql Server 2008 Express which contains 18 million records.
If I declare a variable as 'persistent' in MATLAB, how can I check if
I have a chunk of code where it appears that a variable is changing
Is it possible to declare a variable in Python, like so?: var so that
I have a game which have 5 class/level.Each class contain almost same variables.Now i
I have a problem in Python, for which I cannot find any clean solution

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.