c++ - C++ 函数中的默认参数化是否将默认值压入堆栈?

标签 c++ c++11

如果一个函数有 20 个参数(指针)(示例中使用了 4 个),所有参数的默认值都指向 NULL,这是否意味着每次调用此函数都会在运行时执行 PUSH 以堆栈 NULL 值?

此类函数的示例可能如下所示:

function test(val1=NULL, val2=NULL, .... val20=NULL)

我要求这样做是为了最大限度地提高速度并减少函数调用期间的 # 个周期。

测试用例(这里还有一些额外的东西)

// ArgsListTest.cpp
// @author Mathew Kurian

#include "stdafx.h"
#include <iostream>
#include <time.h>

using namespace std;

// Look at the difference in psuedo-assembly code
// My knowledge in compiler is little, but I can see that
// that there are unncessary cycles being wasted for this part.

// **** With array (this example) *****
// LOAD         Reg, memAddressOfArray
// WRITETOMEM   Reg, ptrToVar1
// INCREMENT    sp
// WRITETOMEM   Reg, ptrToVar2
// DECREMENT    sp
// PUSH         ptrToArray
// JUMP         test

//  ***** IDEALLY SUPPOSED TO LIKE THIS *****
// PUSH        ptrToVar2
// PUSH        ptrToVar1
// JUMP        test

class Base
{
public:
    virtual int test(void* arguments[])
    {
        cout << "Base function being called. VTable lookup ignored since there is no virtual." << endl;
        cout << *static_cast<int*>(arguments[0]) << endl;   // Parameter 1 (Thinks there is only 1 parameter!)

        int x = 5;      x += 1; // Random math to prevent optimizations. (I hope)
        return x;
    }
};

class Derived : public Base
{
public:
    virtual int test(void* arguments[])
    {
        // cout << "Derived function being called. VTable lookup during runtime. Slight overhead here!" << endl;

        // cout << *static_cast<string*>(arguments[0]) << endl; // Parameter 1
        // cout << *static_cast<int*>(arguments[1]) << endl;   // Parameter 2

        int x = 5;      x += 1;
        return x;
    }
};

class Base2
{
public:
    virtual int test(void* arg1 = NULL, void* arg2 = NULL, void* arg3 = NULL, void* arg4 = NULL)
    {
        // cout << "Base2 function being called. VTable lookup ignored since there is no virtual." << endl;

        // cout << *static_cast<string*>(arg1) << endl; // Parameter 1
        // cout << *static_cast<int*>(arg2) << endl;   // Parameter 2
        int x = 5;      x += 1;
        return x;
    }

    virtual int test2()
    {
        int x = 5;      x += 1;
        return x;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base * base = new Derived;
    Base2 * base2 = new Base2;

    int r = 0;
    string * str = new string("sunny");
    int * vale = new int(20);
    int iterations = 1000000000;

    //================================================================================

    printf("Using No-Parameters [%d iterations]\n", iterations);

    clock_t tStart = clock();

    for (int x = 0; x < iterations; x++)
    {
        r = base2->test2();
    }

    printf("Time taken: %.9fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);

    //================================================================================

    printf("Using Array [%d iterations]\n", iterations);

    tStart = clock();

    for (int x = 0; x < iterations; x++)
    {
        void * arguments[] = { str, vale };
        r = base->test(arguments);
    }

    printf("Time taken: %.9fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);

    //================================================================================

    printf("Using Default-Parameters [%d iterations]\n", iterations);

    tStart = clock();

    for (int x = 0; x < iterations; x++)
    {
        r = base2->test(str, vale);
    }

    printf("Time taken: %.9fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);

    //================================================================================

    // cout << "NOTE: Derived class has no extra methods although the parameter counts are different.\n      Parent class doesn't even realize parameter 1 exists!" << endl;

    std::getchar();

    return 0;
}

输出(没有逻辑意义)

enter image description here

为什么第一个测试比第二个慢?创建数组实际上比 PUSH NULL 更快吗?

最佳答案

也许虚拟方法正在影响您的结果。我将您的方法复制到全局范围,这展示了不同的性能顺序。

Using No-Parameters [1000000000 iterations]
Time taken: 24.831000000s
Using Array [1000000000 iterations]
Time taken: 24.730000000s
Using Default-Parameters [1000000000 iterations]
Time taken: 25.241000000s
Using No-Parameters [1000000000 iterations] on int testA()
Time taken: 21.664000000s
Using Array [1000000000 iterations] on int testB(void* arguments[])
Time taken: 22.384000000s
Using Default-Parameters [1000000000 iterations] int testC(void* arg1 = NULL, ...)
Time taken: 22.329000000s

编辑:

将 testB 中数组赋值的相同测试移出 for 循环的范围:

Using No-Parameters [1000000000 iterations]
Time taken: 24.713000000s
Using Array [1000000000 iterations]
Time taken: 24.686000000s
Using Default-Parameters [1000000000 iterations]
Time taken: 25.225000000s
Using No-Parameters [1000000000 iterations] on int testA()
Time taken: 21.653000000s
Using Array [1000000000 iterations] on int testB(void* arguments[])
Time taken: 21.896000000s
Using Default-Parameters [1000000000 iterations] int testC(void* arg1 = NULL, ...)
Time taken: 22.353000000s

关于c++ - C++ 函数中的默认参数化是否将默认值压入堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256831/

相关文章:

c++ - 如何在运行时解开一个 C++ 符号?

c++ - 这是对 unique_ptr 的误用吗?

C++ promise.set_value 在 linux 下失败并出现未知错误

c++ - 是否有 C++11 或 Win32 方法来可靠地生成和保存跟踪信息?

c++ - 为什么在使用模板时出现 "unresolved external symbol"错误?

c++ - 如何在 Mac 上使用 openGL 和 gcc?

c++ - 为什么 C++ 中的 "delete [][]... multiDimensionalArray;"运算符不存在

c++ - 没有括号的 if 语句是否更快?

c++ - 如何将右值引用传递给另一个函数

c++ - 您应该如何为自定义类实现 STL 功能?