Author Topic: What is the fastest C/C++ compiler  (Read 2837 times)

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
What is the fastest C/C++ compiler
« on: March 17, 2019, 05:14:59 AM »
This is a comparative 64-bit performance test between 5 common C/C++ compilers.
I am using the latest versions of the compilers.
All tests, except the CLang, will run on Windows XP 64-bit and above.
All tests run with Fast (i.e. not Precise) Floating Point and are using the default instruction set for 64-bit, which is SSE2. I have optimized for fastest possible speed, but since I don't know much about some of the compilers, for sure results can be improved.
As we can see, there are no large differences in performance under the specified conditions. Still, the clear winner is CLang and the clear loser is the Intel compiler. Of course, testing is always complicated and results will be different under other circumstances.
Now, we all know that in ASM we can get get better performance than this. However, this test has been published in CodeProject 2 years ago and till now nobody ever produced such ASM jewel. I already confessed my inaptitude. But others that downvoted the article could not as well. Strange.


Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
C compiler: Microsoft Visual C/C++ Version: 1916 Toolset: 191627027
Elapsed time: 7670.614100 miliseconds

Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
C compiler: Intel, Version 1900 Build Date: 20190206
Elapsed time: 8395.283300 miliseconds

Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
C compiler: LLVM CLang 7.0 patchlevel 1
Elapsed time: 7327.724200 miliseconds

Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
C compiler: Pelles, Version 900
Elapsed time: 7674.496300 miliseconds

Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
C compiler: MingW64 6.0
Elapsed time: 7698.716100 miliseconds

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6758
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: What is the fastest C/C++ compiler
« Reply #1 on: March 17, 2019, 07:15:58 AM »
With the difference being between 7 and 8 seconds for the whole lot, its pretty much a case of who cares. What I would be interested in seeing is which C compiler produced the best code and best here means fastest. I would trade performance over compile time any time.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: What is the fastest C/C++ compiler
« Reply #2 on: March 17, 2019, 07:23:49 AM »
With the difference being between 7 and 8 seconds for the whole lot, its pretty much a case of who cares. What I would be interested in seeing is which C compiler produced the best code and best here means fastest. I would trade performance over compile time any time.

 :biggrin:
The tests are about performance of the code not compile time of the compilers. I know I was not clear with the title and my English sometimes leaves margin for confusion. Actually, I never bothered with the time compilers or assemblers take, they are always fast enough for me.

daydreamer

  • Member
  • ****
  • Posts: 942
  • watch Chebyshev on the backside of the Moon
Re: What is the fastest C/C++ compiler
« Reply #3 on: March 17, 2019, 07:49:25 AM »
With the difference being between 7 and 8 seconds for the whole lot, its pretty much a case of who cares. What I would be interested in seeing is which C compiler produced the best code and best here means fastest. I would trade performance over compile time any time.

 :biggrin:
The tests are about performance of the code not compile time of the compilers. I know I was not clear with the title and my English sometimes leaves margin for confusion. Actually, I never bothered with the time compilers or assemblers take, they are always fast enough for me.
isnt that why we code directly in assembly,in a philosophic way being the "compiler",when breaking up for example a complex math formula into lots of assembly code? :biggrin:

I dont know how much time a JITcompiler takes before it has compiled all bytecode into machine code,but maybe thats a slowest compiler,because it runs beside the bytecode before it has compiled all bytecode?
first analyzing most freqently called code and compile that first
yes I know it isnt a C compiler,but maybe thats the fastest way to compile to machine code produced to best performance on a wide range cpu's,if its P4's or AMD it uses right opcodes for it to run fastest
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

Manos

  • Regular Member
  • *
  • Posts: 47
Re: What is the fastest C/C++ compiler
« Reply #4 on: March 17, 2019, 09:26:26 AM »

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: What is the fastest C/C++ compiler
« Reply #5 on: March 17, 2019, 10:07:42 AM »
The output looks different because of rand() and xorshift32 in Helper.c. If I understand the source correctly, this benchmark basically measures multiplication performance.

Code: [Select]
Determinant of Matrix using C/C++ is -652805124668261794840576.00.

       Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
C compiler: Microsoft Visual C/C++ Version: 1916 Toolset: 191627027
Elapsed time: 13349.706968 miliseconds

Determinant of Matrix using C/C++ is -3135872705554328936513536.00.

       Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
C compiler: LLVM CLang 7.0 patchlevel 1
Elapsed time: 13640.585769 miliseconds

Determinant of Matrix using C/C++ is 254821845453545970000000.00.

       Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
C compiler: MingW64 6.0
Elapsed time: 13188.805571 miliseconds

Determinant of Matrix using C/C++ is 1860171740789390634745300000000.00.

       Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
C compiler: Pelles, Version 900
Elapsed time: 14703.056519 miliseconds

Determinant of Matrix using C/C++ is -413661966842255395258368.00.

       Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
C compiler: Intel, Version 1900 Build Date: 20190206
Elapsed time: 15497.578726 miliseconds

Caché GB

  • Member
  • **
  • Posts: 89
  • MASM IS HOT
Re: What is the fastest C/C++ compiler
« Reply #6 on: March 17, 2019, 04:14:10 PM »
OK the results on my "mobile gaming desktop".

Code: [Select]
Determinant of Matrix using C/C++ is -45013464764769225408512.00.

Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
C compiler: LLVM CLang 7.0 patchlevel 1
Elapsed time: 7802.865251 miliseconds

----------------------------------------------------------------------------->

Determinant of Matrix using C/C++ is 27506122033733258182656.00.

Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
C compiler: Intel, Version 1900 Build Date: 20190206
Elapsed time: 8932.999451 miliseconds

----------------------------------------------------------------------------->

Determinant of Matrix using C/C++ is -318059265722058350000000.00.

Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
C compiler: MingW64 6.0
Elapsed time: 8200.381124 miliseconds

----------------------------------------------------------------------------->

Determinant of Matrix using C/C++ is -172211128538387334561792.00.

Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
C compiler: Microsoft Visual C/C++ Version: 1916 Toolset: 191627027
Elapsed time: 8425.579861 miliseconds

----------------------------------------------------------------------------->

Determinant of Matrix using C/C++ is 569480412259785342030200000000.00.

Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
C compiler: Pelles, Version 900
Elapsed time: 8309.385132 miliseconds
Caché GB's 1 and 0-nly language:MASM

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: What is the fastest C/C++ compiler
« Reply #7 on: March 17, 2019, 05:49:47 PM »
If I understand the source correctly, this benchmark basically measures multiplication performance.
It tests a few other things like loops and compilers usually make a difference here.
Interesting is that MingW64 (here using gcc 8.1) outperforms CLang in your computer.

I dont know how much time a JITcompiler takes before it has compiled all bytecode into machine code,but maybe thats a slowest compiler,because it runs beside the bytecode before it has compiled all bytecode?
I am not going to make any performance tests for bytecode based compilers:biggrin:

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: What is the fastest C/C++ compiler
« Reply #8 on: March 17, 2019, 07:57:00 PM »
With old AMD:
Code: [Select]
AMD Athlon(tm) II X2 220 Processor
C compiler: LLVM CLang 7.0 patchlevel 1
Elapsed time: 23105.062680 miliseconds

AMD Athlon(tm) II X2 220 Processor
C compiler: Intel, Version 1900 Build Date: 20190206
Elapsed time: 23266.848760 miliseconds

AMD Athlon(tm) II X2 220 Processor
C compiler: Microsoft Visual C/C++ Version: 1916 Toolset: 191627027
Elapsed time: 24822.899920 miliseconds

AMD Athlon(tm) II X2 220 Processor
C compiler: MingW64 6.0
Elapsed time: 25953.812400 miliseconds

AMD Athlon(tm) II X2 220 Processor
C compiler: Pelles, Version 900
Elapsed time: 29015.721800 miliseconds
Support routine with C
Code: [Select]
#include <intrin.h>
char *getcpuBrandString(char *s)
{
_cpuid((int*)s, 0x80000000);
if (*(unsigned int*)s >= 0x80000004) {
_cpuid((int*)s, 0x80000002);
_cpuid((int*)(s+16), 0x80000003);
_cpuid((int*)(s+32), 0x80000004);
}
return s;
}
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: What is the fastest C/C++ compiler
« Reply #9 on: March 17, 2019, 09:40:28 PM »
It tests a few other things like loops and compilers usually make a difference here.
Interesting is that MingW64 (here using gcc 8.1) outperforms CLang in your computer.

The "other things" have apparently not such a big influence on the performance. Which is not surprising. Here is a look under the hood:
Code: [Select]
57*mul msvc
46*mul mingw
48*mul PellesC
45*mul clang
213*mul intel
These muls are almost exclusively mulss instructions. It would be interesting to see the 32-bit results, too - with -arch:SSE or similar, of course.

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: What is the fastest C/C++ compiler
« Reply #10 on: March 17, 2019, 10:20:58 PM »
These muls are almost exclusively mulss instructions. It would be interesting to see the 32-bit results, too - with -arch:SSE or similar, of course.

Up to line 44 it is mostly SSE instructions, namely mulss, addss and subss. Things are different after that.
The same applies in 32-bit when built for SSE or SSE2. In 32-bit(*) it runs over 20% slower when we should expect it to be faster, or am wrong?  :lol: .

(*) I have none built now but will do it if you are curious and don't want to try to build it by yourself.

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: What is the fastest C/C++ compiler
« Reply #11 on: March 17, 2019, 10:28:24 PM »
I am curious to see that, of course. Can you post the exes? Unfortunately, your Main.c throws plenty of errors. I wish C would be compatible to itself :(

Code: [Select]
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Tmp.cpp
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(78): warning C4467: usage of ATL attributes is deprecated
Tmp.cpp(40): error C3861: '_alloca': identifier not found

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: What is the fastest C/C++ compiler
« Reply #12 on: March 17, 2019, 11:11:19 PM »
alloca is in malloc.h in older msvc headers ?
May the source be with you

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: What is the fastest C/C++ compiler
« Reply #13 on: March 17, 2019, 11:23:26 PM »
I am curious to see that, of course. Can you post the exes? Unfortunately, your Main.c throws plenty of errors. I wish C would be compatible to itself :(

Code: [Select]
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Tmp.cpp
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(78): warning C4467: usage of ATL attributes is deprecated
Tmp.cpp(40): error C3861: '_alloca': identifier not found

I am posting the 32-bit tests. Note that the C source code compiled without any change in all compilers. Even better now, because I have replaced the ASM with Timo's suggestion.

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: What is the fastest C/C++ compiler
« Reply #14 on: March 18, 2019, 12:11:48 AM »
Code: [Select]
compiler 64 bit 32 bit gain
Mingw 12.99 14.85 14%
msvc 13.32 22.74 71%
clang 13.4 16.74 25%
Intel 15.25 16.69 9.4%
PellesC 14.61 17.31 19%

How much of that gain is attributable to 64-bit instructions is debatable. For example, the number of mul instructions in the msvc code (the one with the highest gain) is 213 in the 64-bit version but only 46 in the 32-bit exe.

Again, it's a pity that C/C++ is such a mess - cryptic error messages all over the place...
Code: [Select]
C:\TDM-GCC-32\bin\gcc.exe  -O3 -s -o tmp.exe "Tmp.cpp" -lquadmath
____________________
Tmp.cpp:6:33: error: conflicting declaration of 'char* getcpuBrandString(char*)' with 'C' linkage
  char *getcpuBrandString(char *s);
                                 ^
In file included from Tmp.cpp:1:0:
commonHeader_1.h:11:7: note: previous declaration with 'C++' linkage
 char *getcpuBrandString(char *s);
       ^
Tmp.cpp: In function 'float determinant(float*, int)':
Tmp.cpp:40:67: error: '_alloca' was not declared in this scope
   minorMat = (float*)_alloca((rows - 1)*(rows - 1) * sizeof(float));
                                                                   ^