7

It is always said that Python is not so efficient as other languages such as C/C++, Java etc. And it is also recommended to write the bottleneck part in C. But I've never run into such problems, maybe it's because most of the time it is the way you solve the problem rather than the efficiency of the language to bother.

Can anybody illustrate any real circumstances? Some simple codes will be great.

Shady Xu
  • 5,527
  • 2
  • 14
  • 18
  • 1
    Basically, Python is compiled to bytecode and then interpreted in Virtual machine, which makes it (apart from other reasons and architecture) slower than languages like C. But this is only small part of the answer... – Smajl Jun 12 '13 at 08:47
  • Other than the clear answers provided here, you could also have a look at [pypy](http://pypy.org), which is reportedly a lot faster than the normal interpreter in various use cases. – akaIDIOT Jun 12 '13 at 08:49
  • I once wrote a program in python, which took 2000 samples of a file in something like 2 seconds. then I rewrote it in C++, and it took 2000000 samples in less than 2 seconds. – Elazar Jun 12 '13 at 09:07
  • **Efficient**: _Achieving maximum productivity with minimum wasted effort or expense_. Your question has mixed intent and you're conflating raw algorithm / code performance with real world "efficiency" concerns. As you suggest, _it is the way you solve the problem_ that defines the parameters of **effiency** in any given situation. – MattH Jun 12 '13 at 09:20
  • 1
    Try writing any number crunching program in Python first without, then with NumPy, and you'll see the difference. (NumPy is largely C loops.) – Fred Foo Jun 12 '13 at 09:29

3 Answers3

6

There is already an answer to this on SO: Is Python faster and lighter than C++?. It references the Computer Languages Benchmarks Game which I wanted to cite here in the first place.

So Python is (when not using built-in C-code) a lot slower when it comes to doing serious computations.

Community
  • 1
  • 1
Sebastian
  • 5,177
  • 4
  • 30
  • 47
2

A practical comparison using insertion sort, as you can see C is much faster. Note that these are attempts at a 1-to-1, in the real world you would just use Python's sort which uses https://en.wikipedia.org/wiki/Timsort and is far more efficient. Results:

Python

real  0m0.034s
user  0m0.028s
sys   0m0.008s

C

real  0m0.003s
user  0m0.000s
sys   0m0.000s

First in Python

#!/usr/bin/python

a = [16, 7, 4, 10, 18, 15, 6, 12, 13, 5, 11, 14, 17, 8, 2, 9, 19, 3, 1]
print 'Unsorted: %s' % a

def insertion_sort(a):
    for j in range(1, len(a)):
        key = a[j]
        i = j - 1
        while i >= 0 and a[i] > key:
        a[i+1] = a[i]
        i = i - 1
    a[i+1] = key
    return a

# execute the sort
print 'Sorted: %s' % insertion_sort(a)

second in C

#include <stdio.h>
#include <stdlib.h>

/*
    Compile with: 

    cc insertion-sort.c -o insertion-sort
*/
int main(int argc, char **argv) 
{
   int a[20] = {16, 7, 4, 10, 18, 15, 6, 12, 13, 5, 11, 14, 17, 8, 2, 9, 20, 19, 3, 1};
   int i, j, key;
   int len = 20;

   printf("Unsorted: [");
   for ( i = 0; i < len; i++ ) {
       printf(" %d ", a[i]);
   }
   printf("]\n");

   for ( j = 0 ; j < len ; j++ ) 
   {
       key = a[j];
       i = j - 1;
       while ( i >= 0 && a[i] > key ) {
           a[i + 1] = a[i];
           i = i - 1;
       }    
       a[i + 1] = key;
   }

   printf("Sorted: [");
   for ( i = 0; i < len; i++ ) {
       printf(" %d ", a[i]);
   }
   printf("]\n");
}
igniteflow
  • 8,404
  • 10
  • 38
  • 46
1

There isn't a specific set of circumstances in which C or C++ win. Pretty much any CPU-heavy code you write in C or C++ will run many times faster than the equivalent Python code.

If you haven't noticed, it's simply because, for the problems you've had to solve in Python, performance has never been an issue.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Can't agree more. However, what I'm expecting more is some examples to show the **bottlenecks** which really should be rewritten in other languages. – Shady Xu Jun 12 '13 at 08:49