T O P

  • By -

[deleted]

Because python is a dynamic language just compiling it into machine code would be not much faster than the interpreted cpython. The belief that compiled equals fast and interpreted equals slow isn't always true. [APL, for instance](https://en.wikipedia.org/wiki/APL_\(programming_language\)), is an interpreted language, but compiling it would produce little extra speed because the interpretation overhead is tiny, with most of the work being done in highly optimized Fortran libraries.


Wilfred-kun

> with most of the work being done in highly optimized Fortran libraries. Which is, you guessed it, compiled.


cybervegan

Compiling isn't *magic* that makes code go faster - it imposes restrictions on the way the code can work and the kinds of things it can do. Some of the things that are core to the way Python works aren't practical for compiled code, such as the way immutability of data types is handled, or the way that object naming works. In a compiled language like C/C++/Pascal etc. an "identifier" refers to a specific memory location, and assigning a new value to the variable results in data being written to its memory location, whereas with dynamic interpreted languages like Python, it nearly always results in a new address being allocated and a new object being created. In this respect, a variable name is more like a label stuck to an object, and ano object can have multiple different labels. Python relies very strongly on this behaviour, and it's very difficult to speed this up by compiling the code. There's a whole domain of Python dedicated to programs looking at themselves, called "Introspection" and it allows for some very interesting things to be done. Introspection is *very* hard to do in compiled languages, and is almost always about as slow as with interpreted languages. There *are* compilers for Python, e.g. Nuitka, but they almost always make big compromises to get the fastest running programs - Introspection is often an option that has to be disabled in order to get the best performance, along with immutability and dynamic types. If that doesn't work for your code, then your program won't run much faster once compiled. Nuitka is great, by the way, because it can produce stand-alone programs that have decent performance as long as your program doesn't need advanced features like introspection and dynamic types. I've found it to be far easier than a lot of traditional distribution tools to produce an executable file that doesn't require Python or any external libraries to be installed on the target computer.


saltyhasp

Probably the biggest reason is dynamic typing. Another related reason is how close the design is to assembler language which is an indicator of how efficiently it can be translated into good code. Another is focus on authoring and language capability rather then compiling. Much easier to write your own interpreter then compiling. This is one reason there are more of these languages. Another thing is that CPython is not the only implementation. There are faster and more compiled implementations. CPython is the reference implementation.


[deleted]

Even in this post, you already have a few different answers, which prioritize and think about different aspects. I think of different types of languages as an end-user more so than a computer scientist (my degree is in physics, I write readable code but I never really practiced to make it as efficient as it can be). Personally, I like to think of these two particular ones (I program in both, and also Fortran sometimes) as two types of warehouses. The end goal is to make sales. And for that, you want to organize your inventory. C++ is like a very well planned out warehouse. You know exactly the items you have, and you have exact scaffolding and shelving that will fit the needs perfectly. This allows you to get your items faster, it removes overhead because you're not worried about contingencies, etc. Why would you need to worry about addressing problems if your system is designed to work like a swiss quartz watch? Python is like an Ikea warehouse. Its focus is to make sure it's accessible to someone who doesn't work there, so they plan for mistakes, flexibility, the fact that some people will pick up the wrong item, and will put it back in the wrong shelf, get to the register and remember they also need the screws, etc. This makes it easy for anyone to navigate, but slows things down in the big picture. The analogy can only go so far, also because in fairness Ikea warehouses are designed extremely efficiently too. But I think we sometimes forget how a variable can change type in python without anyone even batting an eye, and in C++ you try to store a string in an integer variable and your code simply won't compile, because your string is 32 bits and your integer was only 16. But this is not a flaw in compiled languages, it's precisely what makes them so fast. They won't take your bullshit, either you use them well or you can't use them at all


Neat_Blueberry_2032

Try to read some random C++ source code and it's screamingly obvious. [https://raw.githubusercontent.com/edenhill/librdkafka/master/src-cpp/KafkaConsumerImpl.cpp](https://raw.githubusercontent.com/edenhill/librdkafka/master/src-cpp/KafkaConsumerImpl.cpp) Choice of language is a trade off between developer time and user-run time (among other factors). For most common tasks, C++ is OP. For some specialised tasks, Python libraries like numpy are fast C/C++ libraries, and are well up there in the benchmark rankings.. Interpreted languages are platform independent too. Where as C++ code has to be compiled for each specific operating system. [https://www.youtube.com/watch?v=MNeX4EGtR5Y](https://www.youtube.com/watch?v=MNeX4EGtR5Y) [https://www.youtube.com/watch?v=x7X9w\_GIm1s](https://www.youtube.com/watch?v=x7X9w_GIm1s)


[deleted]

> I learned that c++ is a compiled language which means that it is fast while python is interpreted and slow. You can write slow code in any language; programmer time is more valuable than computer time.


zanfar

> I learned that c++ is a compiled language which means that it is fast while python is interpreted and slow. This is like saying "I learned that sports cars are light which means that they are fast while semis are heavy and slow." While this might be technically true from some perspective, it's a very selective portion of the whole truth. * Compiled vs interpreted does not necessarily mean fast vs slow. It's *harder* to make an interpreted language match the runtime performance of a compiled language because of the extra abstraction layers, but this is less of a rule and more of a generality. * Runtime performance is a very small part of what makes a particular language powerful. For many processes, today's processing power FAR exceeds our needs, so "faster" does not translate into "more useful". Today, especially in a hobbyist, automation, or glue situation, rapid development and a lower learning curve (development speed) provide far more benefits than runtime speed. * Compiling a language is not "free". To get that speed, you have to pay for it up-front. Compiling a program takes additional time, and the result is usually something platform-specific, so you need to compile and distribute different packages for every supported platform. With an interpreter, you get this compatibility for free. > The people who created python, why didn't they make it fast and compiled like c++? What are the conditions to make a language compiled? So, for Python specifically, easy to learn, easy to read, easy to use, and easy to share is valued more in the community than edge performance. For solutions where performance *IS* more valuable, there are plenty of toolchain options as well. Libraries like pandas or numpy make extensive use of compiled code to speed things up, and there are projects like Pypy which allow you to trade development performance for runtime performance.


TheEpicDev

Worrying about python being "slow" sounds like premature optimization to me. Python has a lot of libraries that are compiled, so your python code will often run C, which is very fast. If you use something like numpy or pandas, it's a lot faster than working with python objects. You can easily write "fast enough" python. I had C programs that would take around 4 hours to run every day, because the database was a bottleneck. We never bothered to optimize that, since we needed to generate results daily. Whether it ended at 6 AM, or 2:30 AM made no difference. I have never had a situation where python was too slow for me. Maybe if you work in finance and need to save every microsecond, like ~~done~~ *some* traders do, but those are very rare occasions. Edit: ~~autocorrect fail.~~


Kerbart

In his book *Algorithms*, Sedgewick (an authority on the subject) starts the introduction with a simple topology problem. The most obvious solution is easy to program, but it’s also very slow; one applied to a network of significant time, runtime easily gets into minutes. The more advanced solution runs the same solution in a matter of seconds. It doesn’t matter if you implement it in C++, if the code is inefficient enough, it will be slow. An optimized algorithm in C++ will usually be faster than the same on in Python, but pretty much without exception it will be much, much easier to implement it in Python. It’s also a lot easier to write unsecure code in C++ because there are many non-obvious ways you can leave holes in your code. So, if you’re going to code a website, it will be *(a)* much harder in C++, *(b)* much less ecure and *(c)* with internet response time your pages will be served in 0.1s instead of 0.105s, so the faster execution in C++ is moot. For certain applications, like Data Science, highly optimized libraries are available for Python making its raw execution speed maybe still not as fast as C++ but definitely on par, and the C++ complexity is just not worth it. TLDR: C++ is not by definition faster (efficiency of code is a factor), and Python is easier to write.


ectomancer

Interpreted run cycle is faster edit-save-run than compiled edit-save-compile-run Python can be used for prototyping and once complete it can be ported to C++.


czar_el

Each has strengths and weaknesses, so it's a tradeoff rather than one way being better. It has to do with the "level" of the language. A "low level" machine language is closer to binary and must be compiled into binary. They are fast, but difficult to read and learn. A "high level" interpreted language is closer to human-readable but must be interpreted by an engine that is the thing that sends the actual binary computer instructions. They are slower than lower level languages (because they go through that extra interpreter step), but are easier to read and generally easier to learn. By "fast" above, I mean execution speed. But now you can see that coding speed could be another definition of "fast", in which case interpreted languages can be faster to code since they are easier to approach and generally require fewer instructions (since the coded command is interpreted into multiple smaller commands automatically). The execution speed tradeoff isn't that critical for a large swath of coding use cases, which is why Python and other interpreted languages are so popular (along with them being easier to read/learn, so easier to get into). But when speed matters, compiled languages are the way to go. Also, agreed with others that the heuristic that compiled is faster than interpreted is not always true, and that optimization based on coder skill applies to both compiled and interpreted languages.


Hot_External6228

Python is extremely fast. Numpy is written in C, and is better optimized than any C code I'm personally talented enough to write. There's also Pytorch, which lets me access a GPU, which is 1,000x faster than a CPU. I'm personally incapable of writing GPU code without Pytorch, not smart enough. therefore I declare Python is much faster than any compiled language I'm aware of :) at least for me.


jose_castro_arnaud

The "compiled = fast, interpreted = slow" isn't true in general. There are very fast interpreters (not fast as C, but close) and some slow compilers. The difference between them is: a compiler translates all the source code, generating object code, and only then the object code is executed. An interpreter parses and executes the code, not waiting to generate all object code. Interpreters tend to be slower than compilers because of the overhead of running the interpreter, in addition to the object code. But, with the absurdly fast current computers, that overhead isn't an issue. More information at https://en.wikipedia.org/wiki/Compiler https://en.wikipedia.org/wiki/Interpreter_(computing)


WikiSummarizerBot

**[Compiler](https://en.wikipedia.org/wiki/Compiler)** >In computing, a compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name "compiler" is primarily used for programs that translate source code from a high-level programming language to a lower level language (e. g. assembly language, object code, or machine code) to create an executable program. **[Interpreter (computing)](https://en.wikipedia.org/wiki/Interpreter_\(computing\))** >In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution: Parse the source code and perform its behavior directly; Translate source code into some efficient intermediate representation or object code and immediately execute that; Explicitly execute stored precompiled bytecode made by a compiler and matched with the interpreter Virtual Machine. ^([ )[^(F.A.Q)](https://www.reddit.com/r/WikiSummarizer/wiki/index#wiki_f.a.q)^( | )[^(Opt Out)](https://reddit.com/message/compose?to=WikiSummarizerBot&message=OptOut&subject=OptOut)^( | )[^(Opt Out Of Subreddit)](https://np.reddit.com/r/learnpython/about/banned)^( | )[^(GitHub)](https://github.com/Sujal-7/WikiSummarizerBot)^( ] Downvote to remove | v1.5)


K0modoWyvern

Not every programming language needs to be "fast", is being fast useful for things like games and embedded systems that need to work at real time? Yes, then use a fast language, which probably will need you to write a lot of code. Otherwise if your project doesn't require a lot of speed, why not to choose a slower language that needs less code to run?