Performance differences between Python and C

后端 未结 14 1130
闹比i
闹比i 2020-12-31 08:02

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

相关标签:
14条回答
  • 2020-12-31 08:31

    Across all programs, it isn't really possible to say whether things will be quicker or slower on average in Python or C.

    For the programs that I've implemented in both languages, using similar algorithms, I've seen no improvement (and sometimes a performance degradation) for string- and IO-heavy code, when reimplementing python code in C. The execution time is dominated by allocation and manipulation of strings (which functionality python implements very efficiently) and waiting for IO operations (which incurs the same overhead in either language), so the extra overhead of python makes very little difference.

    But for programs that do even simple operations on image files, say (images being large enough for processing time to be noticeable compared to IO), C is enormously quicker. For this sort of task the bulk of the time running the python code is spent doing Python Stuff, and this dwarfs the time spent on the underlying operations (multiply, add, compare, etc.). When reimplemented as C, the bureaucracy goes away, the computer spends its time doing real honest work, and for that reason the thing runs much quicker.

    It's not uncommon for the python code to run in (say) 5 seconds where the C code runs in (say) 0.05. So that's a 100x increase -- but in absolute terms, this is not so big a deal. It takes so much less longer to write python code than it does to write C code that your program would have to be run some huge number of times to turn a time profit. I often reimplement in C, for various reasons, but if you don't have this requirement then it's probably not worth bothering. You won't get that part of your life back, and next year computers will be quicker.

    0 讨论(0)
  • 2020-12-31 08:35

    In general IO bound work will depend more on the algorithm then the language. In this case I would go with Python because it will have first class strings and lots of easy to use libraries for manipulating files, etc.

    0 讨论(0)
  • 2020-12-31 08:37

    The excess time to write the code in C compared to Python will be exponentially greater than the difference between C and Python execution speed.

    0 讨论(0)
  • 2020-12-31 08:38

    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.

    0 讨论(0)
  • 2020-12-31 08:39

    You will find C is much slower. Your developers will have to keep track of memory allocation, and use libraries (such as glib) to handle simple things such as dictionaries, or lists, which python has built-in.

    Moreover, when an error occurs, your C program will typically just crash, which means you'll need to get the error to happen in a debugger. Python would give you a stack trace (typically).

    Your code will be bigger, which means it will contain more bugs. So not only will it take longer to write, it will take longer to debug, and will ship with more bugs. This means that customers will notice the bugs more often.

    So your developers will spend longer fixing old bugs and thus new features will get done more slowly.

    In the mean-time, your competitors will be using a sensible programming language and their products will be increasing in features and usability, rapidly yours will look bad. Your customers will leave and you'll go out of business.

    0 讨论(0)
  • 2020-12-31 08:41

    (Assumption - The question implies that the author is familiar with C but not Python, therefore I will base my answer with that in mind.)

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

    C will almost certainly be faster unless it is implemented poorly, but the real questions are:

    • What are the development implications (development time, maintenance, etc.) for either implementation?
    • Is the performance benefit significant?

    Learning Python can take some time, but there are Python modules that can greatly speed development time. For example, the csv module in Python makes reading and writing csv easy. Also, Python strings, arrays, maps, and other objects make it more flexible than plain C and more elegant, in my opinion, than the equivalent C++. Some things like network access may be much quicker to develop in Python as well.

    However, it may take time to learn how to program Python well enough to accomplish your task. Since you are concerned with performance, I suggest trying a simple task, such as sorting a text file, in both C and Python. That will give you a better baseline on both languages in terms of performance, development time, and possibly maintenance.

    0 讨论(0)
提交回复
热议问题