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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:17:45+00:00 2026-05-19T23:17:45+00:00

What’s more efficient? decimal value1, value2, formula This: for(int i = 0; i>1000000000000; i++);

  • 0

What’s more efficient?

decimal value1, value2, formula

This:

for(int i = 0; i>1000000000000; i++);
 {
        value1 = getVal1fromSomeWhere();
        value2 = getVal2fromSomeWhere(); 
        SendResultToA( value1*value2 + value1/value2);
        SendResultToB( value1*value2 + value1/value2);
 }

Or this:

for(int i = 0; i>1000000000000; i++)
{
        value1 = getVal1fromSomeWhere();
        value2 = getVal2fromSomeWhere();
        formula = value1*value2 + value1/value2;
        SendResultToA(formula);
        SendResultToA(formula);
}

Intuitively I would go for the latter…

I guess there’s a tradeoff between having an extra-assignment at each iteration (decimal, formula) and performing the computation on and on with no extra-variable…

EDIT :
Uhhh. God… Do I Have to go through this each time I ask a question ?
If I ask it, it is because YES it DOES MATTER to me, fellows.
Everybody does not live in a gentle non-memory-critical world, WAKE-UP !
this was just an overly simple example. I am doing MILLIONS of scientific computation and clouding multithreaded stuff, do not take me for a noob 🙂

So YES, DEFINITELY every nanosecond counts.

PS : I almost regret C++ and pointers. Automatic Memory Management and GC’s definitely made developers ignorant and lazy 😛

  • 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-19T23:17:46+00:00Added an answer on May 19, 2026 at 11:17 pm

    First of all profile first, and only do such micro optimizations if it’s necessary. Else optimize for readability. And in your case I think the second one is easier to read.

    And your statement that the second code has an additional assignment isn’t true anyways. The result of your formula needs to be stored into a register in both codes.
    The concept of the extra variable isn’t valid once the code is compiled. For example in your case the compiler can store formula in the register where value1 or value2 was stored before, since their lifetimes don’t overlap.

    I wouldn’t be surprised if the first one gets optimized to the second one. I think this optimization is called “Common subexpression folding”. But of course it’s only possible if the expression is free of side-effects.

    And inspecting the IL isn’t always enough to see what gets optimized. The jitter optimizes too. I had some code that was quite ugly and slow looking in IL, but very short in the finally generated x86 code. And when inspecting the machine code you need to make sure it’s actually optimized. For example if you run in VS even the release code isn’t fully optimized.

    So my guess is that they are equally fast if the compiler can optimize them, and else the second one is faster since it doesn’t need to evaluate your formula twice.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.