4

It seems to me that languages that are quite simple to use (i.e. Python) often have slower execution times than languages that are deemed more complex to learn (i.e. C++ or Java). Why? I understand that part of the problem arises from the fact that Python is interpreted rather than compiled, but what prevents Python (or another high-level language) from being compiled efficiently? Is there any programming language that you feel does not have this trade off?

Madison May
  • 2,723
  • 3
  • 22
  • 32
  • "slower" languages do more behind the scenes - so they are easier to use (for example they manage the memory for you - this requires more processing time and memory) – Gir Aug 10 '12 at 20:56
  • But what exactly is Python doing behind the scenes? – Madison May Aug 10 '12 at 20:57
  • What's more they have to do the behind the scenes work in a generic way. They have to deal with literally anything a programmer can throw at them. But a language that let's the programmer do some of this work (e.g. memory management) also lets them code a solution that works best for their particular situation. – jahhaj Aug 10 '12 at 20:59
  • 3
    @MadisonMay: I don't know Python, but if it's like other scripting languages, the two big things will be run time type checking and garbage collection. – jahhaj Aug 10 '12 at 21:00
  • 1
    Sigh. Here's another significant question, with reasonably well defined answers that lead the questioner, with luck, to a deeper understanding. Several substantive and upvoted answers. Closed because it "isn't a good fit for the Q&A format", being composed only of an interesting question and several useful answers. – Charlie Martin Aug 13 '12 at 05:04
  • Yeah, bummer. I knew there was a chance that this might happen, but I decided to give it a go anyhow. – Madison May Aug 13 '12 at 21:41

4 Answers4

5

The problem with efficiency in high-level languages (or, at least, the dynamic ones) stems from the fact that it's usually not known WHAT operations needs to be performed until the actual types of objects are resolved in runtime. As a consequence, these languages don't compile to straightforward machine code and have to do all the heavy lifting behind the covers.

Karol Nowak
  • 662
  • 3
  • 8
  • Thanks for the help, Karol. But could something as simple as a few type declarations could cause the 100X speed-up from Python to C++ that is sometimes seen? EDIT: I guess garbage collection needs to be factored in as well. – Madison May Aug 11 '12 at 01:57
5

Lets compare C and Python. By most accounts C is more "complex" to program in than say, Python. This is because Python automates a lot of work which C doesn't. For example, garbage collection is automated in Python, but is the programmer's responsibility in C.

The price of this automation is that these "high level features" need to be generic enough to "fit" the needs of every program. For example, the Python garbage collector has a predefined schedule/garbage collection algorithm, which may not be the optimal for every application. On the other hand, C gives the programmer complete flexibility to define the GC schedule and algorithm as she wants it to be.

So there you have it, ease versus performance.

Sachin
  • 915
  • 6
  • 10
2

Basically, there is a core reason underneath this all: the number of instructions it takes to do something.

In straight old C, most statements can be done with just a few instructions, normally from just over 1 to about 4, and an everage that can be quite close to 1.

In C++, there is more going on under the covers, eg, it takes an extra couple of instructions to invoke a virtual method. Come of this can be resolved by a good optimizer, but run time type information can thwart the optimizer.

In Java, you have the same problems, plus the code is run in an interpreter that expands each JVM instruction into one or more machine instructions -- many more in some cases, especially if the code doesn't run long enough to take advantage of run time opimization.

And Python has all those problems, but it must be parsed at start up and has a move flexible (ergo more complicated) system of dealing with types, which means more instructions yet.

Yes, Python could be compiled. There are even projects that are trying it. But hard compilation loses you some of the advantages of dynamic languages.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • "But hard compilation loses you some of the advantages of dynamic languages." -Like the luxury of leaving out type declarations? – Madison May Aug 11 '12 at 02:01
  • That's one, although type inference gives that back (see, eg, Scala), but also the ease of writing code that writes code, the ease of writing domain specific sublanguages, the ability to easily use a REPL to experiment, the ability to modify classes at run time, the ease of building flexible programming environments. You may not like using those things, but some people find them useful, and it's damn sure that they're hard to get in C++. – Charlie Martin Aug 13 '12 at 05:00
1

Python code execution time is slower that C++ mainly because it is a dynamically typed language. See for instance:

Why are dynamically typed languages slow

Community
  • 1
  • 1
reptilicus
  • 10,290
  • 6
  • 55
  • 79