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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:37:13+00:00 2026-05-27T23:37:13+00:00

If you have a method such as this: float method(myClass foo) { return foo.PrivateVar()

  • 0

If you have a method such as this:

float method(myClass foo)
{
    return foo.PrivateVar() + foo.PrivateVar();
}

is it always faster/better to do this instead?:

float method(myClass foo)
{
    float temp = foo.PrivateVar();
    return temp + temp;
}

I know you’re not supposed to put a call like foo.PrivateVar() in a for loop for example, because it evaluates it many times when you actually only need to use the value once (in some cases.

for (int i = 0; i < foo.PrivateInt(); i++)
{
    //iterate through stuff with 'i'
}

from this I made the assumption to change code like the first example to that in the second, but then I’ve been told by people to not try to be smarter than the compiler! and that it could very well inline the calls.

I don’t want to profile anything, I just want a few simple rules for good practice on this. I’m writing a demo for a job application and I don’t want anyone to look at the code and see some rookie mistake.

  • 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-27T23:37:14+00:00Added an answer on May 27, 2026 at 11:37 pm

    That completely depends on what PrivateVar() is doing and where it’s defined etc. If the compiler has access to the code in PrivateVar() and can guarantee that there are no side effects by calling the function it can do CSE which is basically what you’ve done in your second code example.

    Exactly the same is true for your for loop. So if you want to be sure it’s only evaluated once because it’s a hugely expensive function (which also means that guaranteeing no side-effects get’s tricky even if there aren’t any) write it explicitly.

    If PrivateVar() is just a getter, write the clearer code – even if the compiler may not do CSE the performance difference won’t matter in 99.9999% of all cases.

    Edit: CSE stands for Common Subexpression eliminiation and does exactly what it stands for 😉 The wiki page shows an example for a simple multiplication, but we can do this for larger code constructs just as well, like for example a function call.

    In all cases we have to guarantee that only evaluating the code once doesn’t change the semantics, i.e. doing CSE for this code:

    a = b++ * c + g;
    d = b++ * c * d;
    

    and changing it to:

    tmp = b++ * c;
    a = tmp + g;
    d = tmp * d;
    

    would obviously be illegal (for function calls this is obviously a bit more complex, but it’s the same principle).

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

Sidebar

Related Questions

Why does Java not have a file copy method? This seems like such an
If I have a method in MyClass such as setSuperClassList(List<Superclass>) ...should I be able
I have a method which takes params object[] such as: void Foo(params object[] items)
If I have a method such as: private function testMethod(param:string):void { // Get the
If I have a method such as: public void MyMethod(int arg1, string arg2) How
I have often encountered an error such as cannot convert from 'method group' to
I have only simple data types in method signature of service (such as int,
I am looking for a method of storing Application Messages, such as You have
When you have a method such as the following: public synchronized void addOne() {
I'm looking to write unit tests for a method such as this one: public

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.