[Solved]: Why do we have to trade abstraction for speed?

Problem Detail: Why can high-level languages seemingly never reach lower-level languages in terms of speed? Examples of high-level languages would be Python, Haskell, and Java. Low-level languages would be trickier to define, but let’s say C. Comparisons can be found all around the Internet$^1$ and they all agree that C is a lot faster, sometimes by a factor of 10 or more. What is it that causes such big difference in performance and why can’t high-level languages catch up? Initially I believed that this is all the compilers’ fault and that things will improve in the future, but some of the most popular higher-level languages have been around for decades and they still lag behind when it comes to speed. Can’t they simply compile to a similar to C syntax tree and then follow the same procedures that generate the machine code? Or perhaps it’s something to do with the syntax itself? $^1$ Examples:

Asked By : goblin

Answered By : jmite

Debunking some myths

  1. There is no such thing as a fast langauge. A language can generally produce fast code, but different languages will excel on different benchmarks. We can rank languages on a particular set of flawed benchmarks, but cannot rank languages in a vacuum.
  2. C code tends to be faster because people who need every inch of performance use C. A statistic that C is faster “by a factor of 10” might be inaccurate, because it might be that the people using Python just don’t care as much about speed and didn’t write optimal Python code. We see this in particular with languages like Haskell. If you try really hard, you can write Haskell that performs on par with C. But most people don’t need that performance, so we have a bunch of flawed comparisons.
  3. Sometimes, it is unsafety, not abstraction, that makes C fast. Its lack of array-bounds and null-pointer checks save time, and have been the cause of numerous security holes throughout the years.
  4. Languages aren’t fast, implementations are fast. Many abstract languages start slow, because speed is not their goal, but get faster as more and more optimizations are added.

The abstraction vs speed tradeoff is perhaps inaccurate. I would suggest a better comparison:

Simplicity, speed, abstraction: Choose two.

If we’re running identical algorithms in different languages, the question of speed comes down to the problem of “How much stuff do we need to do at runtime to make this work?” In a highly abstract language that is simple, such as Python or JavaScript, because we don’t know about the representation of data until runtime, there end up being lots of pointer de-referencing and dynamic checks happening at runtime, which are slow. Likewise, there are a lot of checks that need to be done to ensure that you don’t wreck your computer. When you call a function in python, it needs to make sure that the object you’re calling actually is a function, since otherwise you could end up executing random code that does terrible things. Finally, most abstract languages have overhead from garbage collection. Most programmers have agreed that having to track the lifetimes of dynamically allocated memory is a pain, and they’d rather have a garbage collector do it for them at run time. This takes time, that a C program doesn’t need to spend on GC.

Abstract Doesn’t mean slow

However, there are languages which are both abstract and fast. The most dominant in this era is Rust. By introducing a borrow checker and a sophisticated type system, it allows for abstract code, and uses compile-time information to reduce the amount of work that we need to do at runtime (i.e. garbage collection). Any statically typed language saves us time by reducing the number of runtime checks, and introduces complexity by requiring us to please a typechecker at compile-time. Similarly, if we have a language that encodes whether a value can be null into its type system, we can save time with compile time null-pointer checks.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/57904  Ask a Question  Download Related Notes/Documents