为什么Python程序往往比用C或C ++的同等功能的程序慢? [英] Why are Python Programs often slower than the Equivalent Program Written in C or C++?

查看:118
本文介绍了为什么Python程序往往比用C或C ++的同等功能的程序慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Python的似乎慢一些,平均比C / C ++?我学会了Python作为我的第一个编程语言,但我才刚刚开始与C和已经我觉得我可以看到一个明显的区别。

Why does Python seem slower, on average, than C/C++? I learned Python as my first programming language, but I've only just started with C and already I feel I can see a clear difference.

推荐答案

Python是比C高层次的语言,这意味着它抽象从您的计算机的详细信息 - 内存管理,指针等,并允许你写在某种程度上更接近人类是怎么想的方案。

Python is a higher level language than C, which means it abstracts the details of the computer from you - memory management, pointers, etc, and allows you to write programs in a way which is closer to how humans think.

这是事实,C $ C $ç通常运行比Python code快10到100倍,如果你只测量执行时间。但是,如果您还包括开发时间的Python经常跳动C.对于许多项目的开发时间远远超过了运行时性能更重要。较长的开发时间直接转换成额外的成本,更少的功能和上市时间较慢。

It is true that C code usually runs 10 to 100 times faster than Python code if you measure only the execution time. However if you also include the development time Python often beats C. For many projects the development time is far more critical than the run time performance. Longer development time converts directly into extra costs, fewer features and slower time to market.

在内部了Python code执行更慢的原因是因为code是在运行时PTED,而不是在编译时被编译为本地code间$ P $。

Internally the reason that Python code executes more slowly is because code is interpreted at runtime instead of being compiled to native code at compile time.

其他跨preTED语言如Java字节code和.NET字节code运行比Python更快,因为标准发行版包括的 JIT编译器。为什么CPython中没有JIT编译器已经的原因是因为Python的动态特性使得它很难写一个。有一个在的工作://$c$c.google。 COM / p /空载吞咽/>进步来编写一个更快的Python运行,所以你应该期望的性能差距在未来降低,但它可能会需要一段时间Python标准版包含一个强大的前JIT编译器。

Other interpreted languages such as Java bytecode and .NET bytecode run faster than Python because the standard distributions include a JIT compiler that compiles bytecode to native code at runtime. The reason why CPython doesn't have a JIT compiler already is because the dynamic nature of Python makes it difficult to write one. There is work in progress to write a faster Python runtime so you should expect the performance gap to be reduced in the future, but it will probably be a while before the standard Python distribution includes a powerful JIT compiler.

这篇关于为什么Python程序往往比用C或C ++的同等功能的程序慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆