Home > Back-end >  Could you tell me how to do an inline function test program execution time?
Could you tell me how to do an inline function test program execution time?

Time:10-07

Environment: win10 + vs2019

In the Debug mode, the test results as follows:
An inline function called running time: 2.766 seconds
Call an external function running time: 2.658 seconds
Direct running time: 0.232 seconds

In Release mode, the test results as follows:
Inline function called running time is:
0 secondsCall external function running time is:
0 secondsDirect running time is:
0 seconds
there should be some way to make the inline and running time is almost directly, could you tell me how to write test code or on the compiler choose which options?

The original test code:
 # include 
#include
using namespace std;
The inline int comp1 (int a, int b)
{
Return (a & gt; B)? A: b;
}
Int comp2 (int a, int b)
{
Return (a & gt; B)? A: b;
}
Int main ()
{
Double t1, t2;
T1=(double) clock ();
For (int m=0; M & lt; 10000; M++)
For (int n=0; N & lt; 10000; N++)
Comp1 (m, n);
T2=(double) clock ();
Cout & lt;
T1=(double) clock ();
For (int m=0; M & lt; 10000; M++)
For (int n=0; N & lt; 10000; N++)
Comp2 (m, n);
T2=(double) clock ();
Cout & lt; <"Call external function running time is: \ t" & lt; <(t2 - t1)/CLK_TCK & lt; <"Second \ n";

T1=(double) clock ();
For (int m=0; M & lt; 10000; M++)
For (int n=0; N & lt; 10000; N++)
(m & gt; N)? M: n.
T2=(double) clock ();
Cout & lt; <"Direct running time is: \ t" & lt; <(t2 - t1)/CLK_TCK & lt; <"Second \ n";
return 0;
}

CodePudding user response:

The closing of the compiler optimization and dispatch cycle times, project - property - C/C + + - optimization

CodePudding user response:

reference 1/f, blue domain batman reply:
can close the compiler optimization, and dispatch cycle times, project - property - C/C + + - optimization

Optimization properties changed, set "is only applicable to __inline (/Ob1)", useless,
Cycles were large, add a 0, only can increase the operation time, other are all the same

CodePudding user response:

The compiler automatically simple function optimization is inline, 1/f, said ok, can have a try

CodePudding user response:

refer to spend thousands of cassia twig reply: 3/f
compiler automatically simple function optimization is inline, 1/f, said ok, you can try

Optimization properties changed, set "is only applicable to __inline (/Ob1)", useless,
Cycles were large, add a 0, only can increase the operation time, other are all the same

CodePudding user response:

The first half of the sentence you fine goods, no matter you have declared inline, will compile it into inlining.
You put the function is declared inside a local variable, in the test

CodePudding user response:

 
#include
#include

The inline int comp1 (int a, int b)
{
Return (a & gt; B)? A: b;
}

Int comp2 (int a, int b)
{
Return (a & gt; B)? A: b;
}

Int main ()
{
Double t1, t2;
Volatile int dummy;

T1=(double) clock ();
For (int m=0; M & lt; 100000; M++)
For (int n=0; N & lt; 10000; N++)
Dummy=comp1 (m, n);
T2=(double) clock ();
Printf (" inline function called running time is: %. \ n "3 f seconds, (t2 - t1)/CLK_TCK);

T1=(double) clock ();
For (int m=0; M & lt; 100000; M++)
For (int n=0; N & lt; 10000; N++)
Dummy=comp2 (m, n);
T2=(double) clock ();
Printf (" call external function running time is: %. \ n "3 f seconds, (t2 - t1)/CLK_TCK);

T1=(double) clock ();
For (int m=0; M & lt; 100000; M++)
For (int n=0; N & lt; 10000; N++)
Dummy=(m & gt; N)? M: n.
T2=(double) clock ();
Printf (" directly running time is: % 3 f s \ n ", (t2 - t1)/CLK_TCK);

return 0;
}

  • Related