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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:54:47+00:00 2026-06-16T01:54:47+00:00

I thought about this: Is there a performance difference in these two practices: Store

  • 0

I thought about this: Is there a performance difference in these two practices:

  1. Store the return value of a function in a temporary variable than
    give that variable as a parameter to another function.
  2. Put the function into the other function.

Specification

Assuming all classes and functions are written correctly.

Case 1.

ClassA a = function1();
ClassB b = function2(a);
function3(b);

Case 2.

function3(function2(function1()));

I know there aren’t a big difference with only one run, but supposed that we could run this a lot of times in a loop, I created some tests.

Test

#include <iostream>
#include <ctime>
#include <math.h>
using namespace std;

int main()
{
   clock_t start = clock();
   clock_t ends = clock();

   // Case 1.
   start = clock();
   for (int i=0; i<10000000; i++)
   {
      double a = cos(1);
      double b = pow(a, 2);
      sqrt(b);
   }
   ends = clock();
   cout << (double) (ends - start) / CLOCKS_PER_SEC << endl;

   // Case 2.
   start = clock();
   for (int i=0; i<10000000; i++)
      sqrt(pow(cos(1),2));
   ends = clock();
   cout << (double) (ends - start) / CLOCKS_PER_SEC << endl;
   return 0;
}

Results

  • Case 1 = 6.375
  • Case 2 = 0.031

Why is the first one is much slower, and if the second one is faster why dont we always write code that way? Anyway does the second pratice has a name?
I also wondered what happens if I create the variables outside the for loop in the first case, but the result was the same. Why?

  • 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-16T01:54:47+00:00Added an answer on June 16, 2026 at 1:54 am

    Break the throw-this-all-away optimization if you want the computational crunch and your numbers become much more consistent. Ensuring the code to get the proper value is actually run and not entirely thrown out, I’ve assigned the results in both tests to a volatile local (which isn’t exactly proper usage of volatile, but does a decent job of ensuring only the value-creation is the significant delta).

    #include <iostream>
    #include <ctime>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        clock_t start;
        volatile double val;
    
        for (int j=1;j<=10;j++)
        {
            // Case 1.
            start = clock();
            for (int i=0; i<2000000; i++)
            {
                double a = cos(1);
                double b = pow(a, 2);
                val = sqrt(b);
            }
            cout << j << ':' << (double) (clock() - start) / CLOCKS_PER_SEC << endl;
    
            // Case 2.
            start = clock();
            for (int i=0; i<2000000; i++)
                val = sqrt(pow(cos(1),2));
            cout << j << ':' << (double) (clock() - start) / CLOCKS_PER_SEC << endl << endl;
        }
        return 0;
    }
    

    Produces the following release-compiled output on my Macbook Air (which is no speed demon by any stretch):

    1:0.001465
    1:0.001305
    
    2:0.001292
    2:0.001424
    
    3:0.001297
    3:0.001351
    
    4:0.001366
    4:0.001342
    
    5:0.001196
    5:0.001376
    
    6:0.001341
    6:0.001303
    
    7:0.001396
    7:0.001422
    
    8:0.001429
    8:0.001427
    
    9:0.001408
    9:0.001398
    
    10:0.001317
    10:0.001353
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a project that’s using many C++11 facilities, and we thought about this
I have this object below: I thought about putting the messages in html like
I thought I'd find more about this topic but I didn't. I have to
I know this might sound easy. I thought about using the first dot(.) which
This question is about a data structure I thought of. It is a dynamic
This is a thought problem, and one I have been wrestling with for about
Is there any difference in performance (in terms of inserting/updating & querying) a table
I know there have been a lot of questions about sql query performance improvement,
I've published my website many times. But didn't think about this though until I
I have tried looking though several of the already asked question about this topic

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.