Speed up compiled programs using runtime information like for example JVM does it?

361 views Asked by At

Java programs can outperform compiled programming languages like C in specific tasks. It is because the JVM has runtime information, and does JIT compiling when necessary (i guess).

(example: http://benchmarksgame.alioth.debian.org/u32/performance.php?test=chameneosredux)

Is there anything like this for a compiled language? (i am interested in C first of all)

After compiling the source, the developer runs it and tries to mimic typical workload. A tool gathers information about the run, and then according to this data, it recompiles again.

3

There are 3 answers

1
Andreas Grapentin On BEST ANSWER

gcc has -fprofile-arcs

from the manpage:

-fprofile-arcs
    Add code so that program flow arcs are instrumented. During execution the 
    program records how many times each branch and call is executed and how many 
    times it is taken or returns. When the compiled program exits it saves this 
    data to a file called auxname.gcda for each source file. The data may be 
    used for profile-directed optimizations (-fbranch-probabilities), or for 
    test coverage analysis (-ftest-coverage).
0
MTilsted On

I don't think the jvm has ever really beaten well optimized C code.

But to do something like that for c, you are looking for profile guided optimization, where the compiler use runtime information from a previous run, to re-compile the program.

0
Mike Crawford On

Yes there are some tools like this, I think it's known as "profiler-guided optimization".

There are a number of optimizations. Importantly is to reduce backing-store paging, as well as the use of your code caches. Many modern processors have one code cache, maybe a second level of code cache, or a second unified data and code cache, maybe a third level of cache.

The simplest thing to do is to move all of your most-frequently used functions to one place in the executable file, say at the beginning. More sophisticated is for less-frequently-taken branches to be moved into some completely different part of the file.

Some instruction set architectures such as PowerPC have branch prediction bits in their machine code. Profiler-guided optimization tries to set these more advantageously.

Apple used to provide this for the Macintosh Programmer's Workshop - for Classic Mac OS - with a tool called "MrPlus". I think GCC can do it. I expect LLVM can but I don't know how.