함수에 20 개의 매개 변수 (포인터) (예제에서 사용 된 4 개)가 있고 모두 NULL을 가리키는 기본값이 있으면이 함수를 호출 할 때마다 런타임 중에 NULL 값을 스택하기 위해 PUSH를 수행한다는 의미입니까?C++ 함수의 기본 매개 변수화가 기본값을 스택으로 푸시합니까?
function test(val1=NULL, val2=NULL, .... val20=NULL)
내가 속도를 최대화하기 위해이 요청하고 함수 호출시 # 사이클을 줄여 오전 :
샘플 등의 기능처럼 보일 수 있습니다.
테스트 케이스 (LOGICAL 이해 그다지)
// 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;
}
표준은 그것을 말하지 않으며 구현 세부 사항입니다. 그러나 대부분의 시간은 그렇습니다. 컴파일 중에 함수가 라이닝되지 않은 경우 호출 규칙을 준수해야합니다. – WiSaGaN
실제로 20 개의 인수가있는 경우, 더 좁은 인터페이스를 고려하거나 "struct"에 대한 참조를 전달할 수 있습니다. –
조기 최적화는 모든 악의 뿌리입니다 –