Python 比 C++ 更快更轻吗? [英] Is Python faster and lighter than C++?

查看:28
本文介绍了Python 比 C++ 更快更轻吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为Python的优势是代码可读性和开发速度快,但是时间和内存使用不如C++.

I've always thought that Python's advantages are code readibility and development speed, but time and memory usage were not as good as those of C++.

这些统计数据让我印象深刻.

关于 Python 与 C++ 的时间和内存使用情况,您的经验告诉您什么?

What does your experience tell you about Python vs C++ time and memory usage?

推荐答案

我认为您对这些统计数据的理解有误.他们表明 Python 比 C++多达 400 倍,并且除了单个案例之外,Python 更像是一个内存占用者.不过,在源代码大小方面,Python 胜出.

I think you're reading those stats incorrectly. They show that Python is up to about 400 times slower than C++ and with the exception of a single case, Python is more of a memory hog. When it comes to source size though, Python wins flat out.

我使用 Python 的经验表明,在进行任何严重的数字运算时,Python 的速度比 C++ 慢 10 到 100 倍.造成这种情况的原因有很多,主要是: a) Python 是解释型的,而 C++ 是编译型的;b) Python 没有原语,包括内置类型(int、float 等)在内的一切都是对象;c) Python 列表可以保存不同类型的对象,因此每个条目必须存储有关其类型的附加数据.这些都严重阻碍了运行时和内存消耗.

My experiences with Python show the same definite trend that Python is on the order of between 10 and 100 times slower than C++ when doing any serious number crunching. There are many reasons for this, the major ones being: a) Python is interpreted, while C++ is compiled; b) Python has no primitives, everything including the builtin types (int, float, etc.) are objects; c) a Python list can hold objects of different type, so each entry has to store additional data about its type. These all severely hinder both runtime and memory consumption.

不过,这不是忽略 Python 的理由.即使有 100 倍的慢度因素,许多软件也不需要太多时间或内存.开发成本是Python以简洁明了的风格取胜的地方.这种开发成本的改善往往超过了额外的 CPU 和内存资源的成本.然而,如果没有,那么 C++ 就会获胜.

This is no reason to ignore Python though. A lot of software doesn't require much time or memory even with the 100 time slowness factor. Development cost is where Python wins with the simple and concise style. This improvement on development cost often outweighs the cost of additional cpu and memory resources. When it doesn't, however, then C++ wins.

这篇关于Python 比 C++ 更快更轻吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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