Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize for a specific machine / processor architecture

In this highly voted answer to a question on the performance differences between C++ and Java I learn that the JIT compiler is sometimes able to optimize better because it can determine the exact specifics of the machine (processor, cache sizes, etc.):

Generally, C# and Java can be just as fast or faster because the JIT compiler -- a compiler that compiles your IL the first time it's executed -- can make optimizations that a C++ compiled program cannot because it can query the machine. It can determine if the machine is Intel or AMD; Pentium 4, Core Solo, or Core Duo; or if supports SSE4, etc.

A C++ program has to be compiled beforehand usually with mixed optimizations so that it runs decently well on all machines, but is not optimized as much as it could be for a single configuration (i.e. processor, instruction set, other hardware).

Question: Is there a way to tell the compiler to optimize specifically for my current machine? Is there a compiler which is able to do this?

like image 546
Beginner Avatar asked Apr 11 '17 13:04

Beginner


2 Answers

For GCC, you can use the flag -march=native. Be aware that the generated code may not run on other CPUs because

GCC uses this name to determine what kind of instructions it can emit when generating assembly code.

So CPU specific assembly can be generated.

If you want your code to run on other CPU types, but tune it for better performance on your CPU, then you should use -mtune=native:

Specify the name of the processor to tune the performance for. The code will be tuned as if the target processor were of the type specified in this option, but still using instructions compatible with the target processor specified by a -mcpu= option.

like image 54
Vitor Avatar answered Nov 06 '22 22:11

Vitor


Certainly a compiler could be instructed to optimize for a specific architecture. This is true of gcc, if you look at the multitude of architecture flags that you can pass in. The same is true to a lesser extent on Visual Studio, as it has the -MACHINE option and /arch options.

However, unlike in Java, this likely means that the generated code is only (safe) to run on that hardware that is being targeted. The assertion that Java can be just as fast or faster only likely holds in the case of generically compiled C++ code. Given the target architecture, C++ code compiled for that specific architecture will likely be as fast or faster than equivalent Java code. Of course, it's much more work to support multiple architectures in this way.

like image 40
MuertoExcobito Avatar answered Nov 07 '22 00:11

MuertoExcobito