我们应该使用临时变量返回函数的值吗? [英] Should we use temporary variables for the returned values of functions?

查看:115
本文介绍了我们应该使用临时变量返回函数的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想到了这一点:这两种做法是否有性能差异:

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


  1. 将函数的返回值存储在临时

  2. 将该函数作为另一个函数的参数。


b

规格



假设所有类和函数都正确写入。

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



情况2。



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.

#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;
}



结果




  • 案例1 = 6.375

  • 案例2 = 0.031

  • 是第一个是慢得多,如果第二个更快为什么不我们总是写代码的方式?无论如何第二个pratice有一个名称?

    我也想知道如果我在第一种情况下在for循环外创建变量,但结果是一样的会发生什么。为什么?

    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?

    推荐答案

    如果您希望计算机崩溃,数字变得更加一致, 。确保代码获得正确的值实际上是运行,并没有完全抛出,我已经将两个测试中的结果分配到一个volatile本地(这不完全正确使用volatile,但是确保只有价值 - 创造的体面工作是显着的增量)。

    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;
    }
    

    在我的Macbook Air上生成以下发布版本的输出恶魔任何弹力):

    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
    

    这篇关于我们应该使用临时变量返回函数的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆