Ask HN: Why do we use interpreters instead of compilers?
Compiled code would run faster, and would be good for the processor cache, why do we still use a lot of interpreted programs at servers and in web world.
Yes JIT compilers are available, but still why are interpreters more popular?
21 comments
[ 4.0 ms ] story [ 31.2 ms ] threadBoth Java and C# are compiled (JIT or AOT depending on environment). PHP is often interpreted for development but pretty regularly compiled for production (see Facebook et al).
And it wasn't a bug or homework assignment or something involving NP?
Not all of that is cpu-shortage but some of it is.
On the other hand, dynamic language interpreters already handle both syntax parsing and execution at runtime. Therefore loading changes during development is not a big deal for them.
Compiled languages are put together to let the programmer work harder to create programs that have some kind of high performance. So the end product can be quicker, even if it more expensive to produce.
Only a few languages try to bridge the gap. Most notably Go. Go is compiled but is also set up to be easy to use. The result is a nice mixture of good performance and ease of programming. See golang.org.
Interpreters make the inner loop of the programmer's life faster, making program development faster.
Once the program works, a profiler can be applied, and only the very slowest things recoded in lower-level language.
For many projects, optimization is not necessary at all.
Even in Donald Knuth's time, "Premature optimization is the root of all evil," was a common caution. Modern hardware makes this even more true today.
Not to say that you should go out of your way to code clumsily, but that shaving a cycle here and there anywhere but the innermost of loops is likely to be a waste of the only time that is really precious any more... your own time writing the code.
Given the amount of time code spends in maintenance, taking the time to write clear code will save the maintainer (possibly you!) time, so readability should be prized over raw execution speed in almost all cases. In six months, you won't remember how that tricky one-liner really works, and will waste reading time in minutes to hours that saved microseconds in execution.
Before LLVM, statically typed, compiled languages got bogged down in a lot of stuff that wasn't language design. They still do though.
There are lot of things on the web today that are compilers: Jekyll and multiple static site generators compile static pages. A cache like Varnish works as the end-point of a process that results in a compiled result, to the same interpretation doesn't need to be done ('compiled').