I run the same code in VS code by internalConsole and externalConsole. But they give different results.
The platform is:
Visual Studio Code:1.64.2(system setup)
OS: Windows NT x64 10.0.19042
gcc: 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
InternalConsole is launched by Coderunner and returns:
PS D:\coding\notes\DataStructure\01> g++ '02duoxiangshi.cpp' -o '02duoxiangshi.exe' -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK ; if ($?) { &'./02duoxiangshi.exe' }
ticks1 = 1346.000000
duration1 = 1.35e-007
ticks2 = 0.000000
duration2 = 0.00e+000
ExternalConsole is launched by pressing F5 and returns:
ticks1 = 1385.000000
duration1 = 1.39e-07
ticks2 = 191.000000
duration2 = 1.91e-08
Problems: The ticks2 and durations2 in the former is 0, which is wrong, while the latter seems right.
I find out it maybe caused by the -O2 option in g++ '02duoxiangshi.cpp' -o '02duoxiangshi.exe' -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK ; if ($?) { &'./02duoxiangshi.exe' }
by testing. By deleting -O2, the result is:
PS D:\coding\notes\DataStructure\01> g++ '02duoxiangshi.cpp' -o '02duoxiangshi.exe' -Wall -m64 -static-libgcc -fexec-charset=GBK ; if ($?) { &'./02duoxiangshi.exe' }
ticks1 = 1489.000000
duration1 = 1.49e-007
ticks2 = 192.000000
duration2 = 1.92e-008
The code file name is 02duoxiangshi.cpp. The code is as follows:
#include <stdio.h>
#include <time.h>
#include <math.h>
#include<stdlib.h>
clock_t start, stop;
double duration;
#define MAXN 10
#define MAXK 1e7
double f1(int n, double a[], double x );
double f2(int n, double a[], double x );
int main ()
{
int i;
double a[MAXN];
for (i = 0; i < MAXN; i++)
{
a[i] = (double)i;
}
start = clock();
for (i = 0; i < MAXK; i++)
{
f1(MAXN - 1, a, 1.1);
}
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK / MAXK;
printf("ticks1 = %f\n", (double)(stop - start));
printf("duration1 = %6.2e\n", duration);
start = clock();
for (i = 0; i < MAXK; i++)
{
f2(MAXN - 1, a, 1.1);
}
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK / MAXK;
printf("ticks2 = %f\n", (double)(stop - start));
printf("duration2 = %6.2e\n", duration);
system("pause");
return 0;
}
double f1( int n, double a[], double x )
{
int i;
double p = a[0];
for (i = 1; i <= n; i++)
{
p += a[i] * pow(x, i);
}
return p;
}
double f2( int n, double a[], double x )
{
int i;
double p = a[n];
for (i = n; i > 0; i--)
{
p = a[i - 1] + x * p;
}
return p;
}
Questions:
- Is it because of the -O2?
- Should I delete it from now on?