Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

I'm a beginner in programming. I am learning Python as a hobby. However, after reading some things in what concerns, for example, its speed, I asked again myself if I should really learn Python or learn other more difficult language.

I am in sciences at school, and have interest in physics and microcontrollers, so a performance-wise language would be needed for, for example, computational calculations, and that. So probably a language like C or C++ would be more handy in terms of future university degree.

So, my questions are:

  • I saw that Python is an interpreted language, therefore slower (up to 100 times from C++, from what I read at Stack Overflow, which is a lot). Can't it be compiled so that no interpreter is needed and it has high speed? (like C is) I mean, it's what the interpreter does, isn't it?

  • Does Python have any chance of being useful for science? How could it surpass it's speed lack?

  • Isn't there the possibility of writing a program in Python, and translate it to C++ in order to compile? Therefore having the simple writing of Python and the speed of C++? Are there any Python-to-C++ translators?

share|improve this question

marked as duplicate by gnat, Martijn Pieters, MichaelT, Glenn Nelson, Mike Brown Mar 15 '13 at 16:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
"...about 100times from C++, from what I read..." and where did you read that? Do you have a link to that source? –  FrustratedWithFormsDesigner Mar 13 '13 at 16:07
1  
here: stackoverflow.com/questions/801657/… –  allg18 Mar 13 '13 at 16:18
2  
Python is used a lot for Science. See SciPy, Sage, and countless people using those and other solutions. As a specific example, CERN has a fair bit of Python IIRC, though mostly for "prototyping". And regarding compilation: See programmers.stackexchange.com/q/181944/7043 and note that your approach to this issue is way too simplistic. –  delnan Mar 13 '13 at 16:31
    
What does a language have to do with a college degree? –  Austin Henley Mar 13 '13 at 18:55
1  

5 Answers 5

This may be a bit too broad but I'll bite

Yes python can be used for science application (see numpy and scipy) , it will depend on the exact application as to whether it is suitable (but this is true for any language).

wrt speed i'd note that

  • the cpu is not necessarily the bottleneck even in scientific applications e.g. I/O is much slower
  • the skills you learn programming with python will be largely transferable to other languages, you can always learn C++ later (and in fact it is beneficial to learn several languages)
  • there is no rule stating you have to use a single language for the entire application, its perfectly possible to do some fast critical section in C then build the rest in python
  • translating to C or C++ is possible but doesn't necessarily get you that much speed increase unless you do it by hand (and do it well).

honestly I think you should get stuck in with python

share|improve this answer
2  
+1 for numpy and general advice. –  khedron Mar 13 '13 at 16:16
2  
Translating to C or C++ by hand doesn't get you nearly as much speed increase as one would hope either if done badly. And it's really easy to do badly. –  delnan Mar 13 '13 at 16:29
    
@delnan yes, very true –  jk. Mar 13 '13 at 16:45

A lot of scientific computing doesn't require a great deal of speed, and you're more interested in making the translation to a programming language as close as possible to the scientific/mathematical representation. Python is very well suited for this, and frequently used, as others have pointed out.

When speed does matter, the programming language is the easiest thing to adapt. You will have to learn things like how to create an algorithm that runs efficiently on a 200 node cluster. Most people find that a lot harder to learn than a new programming language.

To specifically address your comment on which language would be more handy in terms of a future university degree, keep in mind that language use is nowhere near standardized among universities, even within the same discipline, and that they change it all the time. Even different classes in the same degree program will use different languages. My computer science classes used Java, Perl, C, C++, intel assembly, powerpc assembly, pic assembly, mips assembly, pseudocode, ada, lisp, and prolog. On top of that, I had science and engineering classes that did calculations using Mathematica, Matlab, Excel, R, and some obscure tools I can't remember the names of.

My point is, don't get too hung up thinking your choice of language now will in any way limit your options down the road.

share|improve this answer
    
A follow up to this great answer, if you master algorithms, learning a new programming language is pretty simple and something you can do on the fly if you really need to. There's really no reason whatsoever to worry about language choices outside of "is this language (and its features) a good match for the problem I'm trying to solve?" –  Eric Hydrick Mar 14 '13 at 23:42

See this StackOverflow answer for notes on compiled Python code performance. If you need another language like C, you can include it in Python too, here's the official documentation - although you probably won't need to for performance reasons (library compatibilities are another issue though).

share|improve this answer

I agree that Python is a very reasonable choice for scientific applications. The other answers have done a pretty good job refuting the idea that Python runtimes are necessarily slow.

I also want to point out that Python development time is often very fast, which is particularly useful for scientific development, which has a slightly different workflow from "regular" development. Most software is intended to be used repeatedly: people run the same copy of word processors, smartphone apps, and webservers over and over again. On the other hand, a lot of scientific software is run once or twice to perform a specific simulation or analysis. The output is either deemed totally uninteresting or saved as a figure/described in a manuscript, and either way, the author moves on to other things. For situations like these, you are often better off writing slow-ish code quickly than super-efficient code slowly: 1 day to write and run overnight is better for your scientific productivity than spending a week writing the same simulation in a way that lets it run in three hours.

That said, some programs do get used over and over again. For those, it's definitely worth spending the time optimizing (and potentially even switching to a faster platform), but it is exceedingly hard to predict which 10-20% of your code will fall into that category.

Finally, scientific programming also often involves using a bunch of existing tools, written in a mish-mash of (sometimes old or obscure) languages. Python is a very reasonable "glue" language in that it has decent text processing support (for processing these programs' output) plus its own numerical and graphics capabilities, especially if you also use NumPY/Matplotlib.

share|improve this answer

There are quite a few misconceptions in the question.

First of all, I wouldn't view C, C++ and Python as alternatives, rather as complementary languages, each good in it's domain.

Yes, pure Python code is slower. However, most scientific packages (including SciPy/Numpy) are actually in great part implemented in C. So all the number crunching is actually fast. Similarly the Python's standard implementation is C implementation. Meaning that using built-ins is most often just calling fast C code.

OTOH, Python is high-level, 100% object oriented language perfect for rapid development. Need to tweak something in the logic, you'll do it in no time. Also keep in mind that often scalability might be more important than just raw speed. It's easier to write scalable code in a language that is higher level.

What is the perfect solution? Learn Python, learn C and C++. Than learn how to integrate one with another. For C you have two options: to either just implement standard C Python modules using Python's C API, or use C libraries w/o any modifications using ctypes module. For C++ you'd use excellent Boost.Python

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.