by gjm11 · 7y ago · 0 comments
Or consider: Every native-compiled language ends up as machine code, which is equivalent to assembly language, but for most (perhaps all) it would be grossly misleading to say "as fast as assembler".
(A neat tool in one's toolbox, of course. But just transpiling to C does not get one as fast as C.)
Your comparison to machine code (or human readable assembly code) is less useful in that such a statement means very little until one knows how said machine code is being produced (e.g., manually, from a IR, etc.).
That a language is compiled to C does not mean that its compiler is going to be able to produce a C program that's as good as a that well-written C equivalent. (A relatively obvious example would be a compiler that introduces a heavy runtime, and doesn't give the C compiler enough information for it to get rid of the runtime)
It's the same with assembly code: that a compiler produces assembly does not mean the resulting program is fast.
That’s your interpretation, which is fine, but the objective meaning stands. Even the idea of “well-written C” is, in my experience, fairly subjective amongst C programmers.
That some particular C program exists that is at least as slow as a program in some other language is always true, trivially, and so is not a good interpretation of "as fast as C" regardless of its objectivity.
But that doesn't at all follow from compiling to C. I gave one example above; here's another. Perhaps my programming language has arbitrary-length integers and the C code it produces for a simple loop looks something like this:
bignum_t i = bignum_from_int(0);
bignum_t x = bignum_from_int(0);
while (i < bignum_from_int(1000000)) {
bignum_add_in_place(x, bignum_bitand(i, bignum_from_int(1)));
}
Corresponding code in "normal" C would use machine integers and the compiler might well be able to understand the loop well enough to eliminate it altogether. Code like the above would run much, much slower, despite being technically in C.Being as fast as C is not about using C as an intermediate language but having data structures and control flow that resemble what C compiler have been optimized for.
Case in point, Haskell GHC is capable of outputting C code but the algorithm will not get C performance (or Haskell performance without this intermediate C representation).