Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance differences between Python and C

Working on different projects I have the choice of selecting different programming languages, as long as the task is done.

I was wondering what the real difference is, in terms of performance, between writing a program in Python, versus doing it in C.

The tasks to be done are pretty varied, e.g. sorting textfiles, disk access, network access, textfile parsing.

Is there really a noticeable difference between sorting a textfile using the same algorithm in C versus Python, for example?

And in your experience, given the power of current CPU's (i7), is it really a noticeable difference (Consider that its a program that doesnt bring the system to its knees).

like image 578
Pablo Hevia-Koch Avatar asked Aug 20 '10 18:08

Pablo Hevia-Koch


People also ask

Why is C much faster than Python?

C/C++ is relatively fast as compared to Python because when you run the Python script, its interpreter will interpret the script line by line and generate output but in C, the compiler will first compile it and generate an output which is optimized with respect to the hardware.

Why is Python slow compared to C?

Internally Python code is interpreted during run time rather than being compiled to native code hence it is a bit slower. Running of Python script v/s running of C/C++ code: Python: First it is compiled into Byte Code. This Byte Code is then interpreted and executed by the PVM (Python Virtual Machine).

Which is faster C or C++ or Python?

After compiling, if you run the code, it takes about 2.42 seconds to generate all 67 million 13-mers. It means Python takes 25 times more time to run the same algorithm compared to C++.

What are the differences between C and Python?

C is a general-purpose programming language that is extremely popular, simple and flexible. It is machine-independent, structured programming language which is used extensively in various applications. Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.


2 Answers

Use python until you have a performance problem. If you ever have one figure out what the problem is (often it isn't what you would have guessed up front). Then solve that specific performance problem which will likely be an algorithm or data structure change. In the rare case that your problem really needs C then you can write just that portion in C and use it from your python code.

like image 109
jshen Avatar answered Sep 18 '22 12:09

jshen


The first rule of computer performance questions: Your mileage will vary. If small performance differences are important to you, the only way you will get valid information is to test with your configuration, your data, and your benchmark. "Small" here is, say, a factor of two or so.

The second rule of computer performance questions: For most applications, performance doesn't matter -- the easiest way to write the app gives adequate performance, even when the problem scales. If that is the case (and it is usually the case) don't worry about performance.

That said:

  • C compiles down to machine executable and thus has the potential to execute as at least as fast as any other language
  • Python is generally interpreted and thus may take more CPU than a compiled language
  • Very few applications are "CPU bound." I/O (to disk, display, or memory) is not greatly affected by compiled vs interpreted considerations and frequently is a major part of computer time spent on an application
  • Python works at a higher level of abstraction than C, so your development and debugging time may be shorter

My advice: Develop in the language you find the easiest with which to work. Get your program working, then check for adequate performance. If, as usual, performance is adequate, you're done. If not, profile your specific app to find out what is taking longer than expected or tolerable. See if and how you can fix that part of the app, and repeat as necessary.

Yes, sometimes you might need to abandon work and start over to get the performance you need. But having a working (albeit slow) version of the app will be a big help in making progress. When you do reach and conquer that performance goal you'll be answering performance questions in SO rather than asking them.

like image 29
mpez0 Avatar answered Sep 17 '22 12:09

mpez0