N
That's the code.#include <iostream>
#include <conio.h>
#include <math.h>
#include <time.h>
using namespace std;
float func(float x)
{
return xxlog(x)/((1+x)*(1+x));
}
int main(int argc, char** argv)
{
clock_t start = clock();
for (int i=0; i<10000000;i++) func(i);
clock_t stop = clock();
cout << (stop-start) << endl;
}
emits, under normal conditions, a compilation with optimization instantly.Which is not surprising, that's what he's compiling:main PROC ; COMDAT
; 13 : {
$LN13:
push rbx
sub rsp, 32 ; 00000020H
; 14 : clock_t start = clock();
call clock
mov ebx, eax
; 15 : for (int i=0; i<10000000;i++) func(i);
; 16 : clock_t stop = clock();
call clock
; 17 : cout << (stop-start) << endl;
sub eax, ebx
lea rcx, OFFSET FLAT:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
mov edx, eax
call ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV01@J@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<<
; File c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\ostream
; 206 : return ((*_Pfn)(*this));
mov rcx, rax
call ??$endl@DU?$char_traits@D@std@@@std@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@0@AEAV10@@Z ; std::endl<char,std::char_traits<char> >
; File g:\tmp\test\test.cpp
; 18 : }
xor eax, eax
add rsp, 32 ; 00000020H
pop rbx
ret 0
main ENDP
I mean, he's just... No.♪In the sweet version and with inlineand without one, result one:; 15 : for (int i=0; i<10000000;i++) func(i);
mov DWORD PTR i$1[rsp], 0
jmp SHORT $LN4@main
$LN2@main:
mov eax, DWORD PTR i$1[rsp]
inc eax
mov DWORD PTR i$1[rsp], eax
$LN4@main:
cmp DWORD PTR i$1[rsp], 10000000 ; 00989680H
jge SHORT $LN3@main
cvtsi2ss xmm0, DWORD PTR i$1[rsp]
call ?func@@YAMM@Z ; func
jmp SHORT $LN2@main
$LN3@main:
The difference of 200 ms can be easily attributed to the error of the measurements: () - they must be made for any accuracy of 20-30-40, medium and estimate error. ♪ ♪And the point is... inline, like marginalism: not dogma, but leadership. Compiler himself. Decide to pay attention or not. Yes, it can be forced to do this unstandard expansion. inlinebut in general, the compiler is more visible. ♪P.S. In the code inline float func(float x)
{
return xxlog(x)/((1+x)*(1+x));
}
float funs(float x)
{
return xxlog(x)/((1+x)*(1+x));
}
int main(int argc, char** argv)
{
{
double z = 0;
clock_t start = clock();
for (int i=1; i<100000000;i++) z += func(i);
clock_t stop = clock();
cout << (stop-start) << endl;
cout << z << endl;
}
{
double z = 0;
clock_t start = clock();
for (int i=1; i<100000000;i++) z += funs(i);
clock_t stop = clock();
cout << (stop-start) << endl;
cout << z << endl;
}
}
The compilator has inclined both challenges in a safe way.For 40 tests, the time of the first cycle is 568 ± 12 tt, the second to 569 ±14. So within the margin of error, everything OK. Have you ever assessed the error?