Unused default arguments decrease C ++ performance

Suppose I declare a function foo(int arg1, int arg2 = 0, int arg3 = 0, int arg4 = 0). The last three arguments will be indicated only occasionally (if ever), and basically, funciton will be called as foo(some_int). Would I get performance by declaring a function instead foo(int arg1)and having a different solution to pass other arguments if they are really needed?

In other words, do declaring, but undefined arguments by default slow down a function call?

The function in this case is the constructor for the object, but this is a general question.

+4
source share
2 answers

(You can just read the conclusion at the end if you want)

I did a test to check this, I first ran this short program about ten times:

#include <iostream>
#include <ctime>

using namespace std;

int returnMe(int me)
{
    return me;
}


int main()
{
   float begin = (float)clock();
   for(int i = 0; i < 100000000; i++)
   {
       int me = returnMe(i);
   }
   printf("\nTime: %f\n", begin);
   printf("\nTime: %f\n", (float)clock());

   return 0;
}

Which basically performs the function a returnMehundred million times, and then tells me how much time has passed. Values ​​ranged from 280 ms to 318 ms. Then I ran this program:

#include <iostream>
#include <ctime>

using namespace std;

int returnMe(int me, int me87 = 0, int m8e = 0, int m5e = 0, int m34e = 0,int m1e = 0,int me234 = 0,int me332 = 0,int me43 = 0,int me34 = 0,int me3 = 0,int me2 = 0,int me1 = 0)
{
    return me;
}


int main()
{
   float begin = (float)clock();
   for(int i = 0; i < 100000000; i++)
   {
       int me = returnMe(i);
   }
   printf("\nTime: %f\n", begin);
   printf("\nTime: %f\n", (float)clock());

   return 0;
}

about ten times, and the values ​​now range from 584 ms to 624 ms.

Conclusion: Yes, this will call the function slower, but very small. . Creating a separate function to pass other arguments to the object or using another constructor will increase performance, but is it worth it to use additional code?

, Box2D, . , , "", - , . , , . , .

: ( ) . , , , , , . . , 12 .

======== EDIT: .

, g++ test.cpp -o test.exe. , -O0. -O3?

, g++ test.cpp -o test.exe -O3, , 1-2 . , , . , g++, , , , , , , returnMe , , .

, returnMe, , . :

#include <iostream>
#include <ctime>

using namespace std;

long long signed int bar = 0;

int returnMe(int me)
{
    bar -= me;
    return me;
}


int main()
{
   float begin = (float)clock();
   for(int i = 0; i < 1000000000; i++)
   {
       int me = returnMe(i);
       bar -= me * 2;
   }
   printf("\nTime: %f\n", begin);
   printf("\nTime: %f\n", (float)clock());
   printf("Bar: %i\n", bar);

   return 0;
}

#include <iostream>
#include <ctime>

using namespace std;

long long signed int bar = 0;

int returnMe(int me, int me87 = 0, int m8e = 0, int m5e = 0, int m34e = 0,int m1e = 0,int me234 = 0,int me332 = 0,int me43 = 0,int me34 = 0,int me3 = 0,int me2 = 0,int me1 = 0)
{
    bar -= me;
    return me;
}

int main()
{
   float begin = (float)clock();
   for(int i = 0; i < 1000000000; i++)
   {
       int me = returnMe(i);
       bar -= me * 2;
   }
   printf("\nTime: %f\n", begin);
   printf("\nTime: %f\n", (float)clock());
   printf("Bar: %i\n", bar);

   return 0;
}

:

: 653 686

: 652 735

, , .

+2

, .

/ , . , , , ( ).

, - . , , , , , .

+1

Source: https://habr.com/ru/post/1545072/


All Articles