Performance differences between Python and C

后端 未结 14 1113
闹比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:18

    It really depends a lot on what your doing and if the algorithm in question is available in Python via a natively compiled library. If it is, then I believe you'll be looking at performance numbers close enough that Python is most likely your answer -- assuming it's your preferred language. If you must implement the algorithm yourself, depending on the amount of logic required and the size of your data set, C/C++ may be the better option. It's hard to provide a less nebulous answer without more information.

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

    C will absolutely crush Python in almost any performance category, but C is far more difficult to write and maintain and high performance isn't always worth the trade off of increased time and difficulty in development.

    You say you're doing things like text file processing, but what you omit is how much text file processing you're doing. If you're processing 10 million files an hour, you might benefit from writing it in C. But if you're processing 100 files an hour, why not use python? Do you really need to be able to process a text file in 10ms vs 50ms? If you're planning for the future, ask yourself, "Is this something I can just throw more hardware at later?"

    Writing solid code in C is hard. Be sure you can justify that investment in effort.

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

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

    Yes.

    The noticeable differences are these

    1. There's much less Python code.

    2. The Python code is much easier to read.

    3. Python supports really nice unit testing, so the Python code tends to be higher quality.

    4. You can write the Python code more quickly, since there are fewer quirky language features. No preprocessor, for example, really saves a lot of hacking around. Super-experience C programmers hardly notice it. But all that #include sandwich stuff and making the .h files correct is remarkably time-consuming.

    5. Python can be easier to package and deploy, since you don't need a big fancy make script to do a build.

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

    To get an idea of the raw difference in speed, check out the Computer Languages Benchmark Game.

    Then you have to decide whether that difference matters to you.

    Personally, I ended up deciding that it did, but most of the time instead of using C, I ended up using other higher-level languages. Personally I mostly use Scala, but Haskell and C# and Java each have their advantages also.

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

    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.

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

    If your text files that you are sorting and parsing are large, use C. If they aren't, it doesn't matter. You can write poor code in any language though. I have seen simple code in C for calculating areas of triangles run 10x slower than other C code, because of poor memory management, use of structures, pointers, etc.

    Your I/O algorithm should be independent of your compute algorithm. If this is the case, then using C for the compute algorithm can be much faster.

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