Re: Learning to code by reading code
In comp.lang.c++ Rod Pemberton <do_not_have@noavailemail.cmm> wrote:
Even then it would probably be hard to match the speed of an actual
compiler of the original language.
I don't believe that.
Why not? It's not very hard to believe. If the language has no support
for a certain higher-level concept, then the compiler cannot use that
concept to perform optimizations.
You cannot express everything in C that can be expressed in asm.
(You can probably achieve the same functionality, but you cannot achieve
the same efficiency in every single case.)
C has had some of the most intensive research into
language optimizations.
Yes, into *C* language optimizations, not other languages. What C does
not support as a concept the C compiler cannot optimize.
Just take C++ exceptions, for instance. I don't see any easy way
to get the same effect in C without slowing down the code.
Sorry, I'm not familiar with C++. However, if exceptions interrupt the
normal program flow like signals do, then it'll slow down the code purely
due to whatever save-restore state mechanism is used to handle the
exception. That requires substantial overhead in assembly.
Nope. That's the genius in C++ exceptions: Support for exceptions does
not slow down the program in any way (compared to compiling the program
without support for exceptions). (It was, AFAIK, in fact a requirement
by the standardization committee, that exceptions would be added to the
standard only if it's possible to support them without compromising the
speed of the program. It turns out that it's possible.)
(Of course *throwing* an exception has overhead, but that's to be expected.
Exceptions are designed to be used to handle fatal errors, not for normal
operation. The main point is that when no exceptions are thrown, the code
is in no way slower than eg. the equivalent C program would be, even though
exceptions are supported and could be thrown at any moment, at any point
in the code.)