Performance differences between Python and C

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

提交回复
热议问题