python、C++、rust速度比较

TIobe指数依据向主要搜索引擎提交编程语言名称时返回的网页数量来衡量编程语言的流行程度。该指数每月更新一次,并提供了自2002年以来的历史数据。

其官网是https://www.tiobe.com/tiobe-index/

有意思的事情来了,看下图。

这是编程语言排名的tiobe网站首页的截图。

原话是:

复制代码
This month, Python has a ranking of more than 18% for the first time in its history. The last time a language hit more than 18% was Java in November 2016. Java is also the language with the highest ranking ever: 26.49% in June 2001. Runner up C++ is now exactly 8% behind Python, and that difference between position #1 and position #2 is also almost a record. The highest difference ever between position #1 and position #2 was in November 2016 when Java was 9.55% ahead of C. In summary, Python's hegemony is now undeniable. It is likely that it is Python's next step to become the most popular programming language ever. Is there any new language expected to come close to Python soon? Possible contenders Rust and Kotlin are approaching the TIOBE index top 10 fast, but it will take a lot of time before they become a real threat to Python.

我简单机翻一下,快速看看这讲了个啥:

复制代码
2024年8月,Python的排名历史上首次超过了18%。上次有语言超过18%还是在2016年11月的Java。Java也是史上排名最高的语言:在2001年6月达到了26.49%。位列第二的C++目前落后Python正好8%,而这一第一名与第二名之间的差距也几乎是历史纪录。历史上第一名与第二名的最大差距是在2016年11月,当时Java领先C语言9.55%。总而言之,Python的主导地位现在无可否认。
复制代码
Python下一步很可能成为有史以来最受欢迎的编程语言。近期是否有新的语言可能接近Python的地位?潜在的竞争者Rust和Kotlin正在快速接近TIOBE指数前十,但要成为对Python的真实威胁还需要很长时间。
复制代码

复制代码

Python之所以流行,主要有以下五点原因:

  1. 易学性:Python语法简单清晰,适合编程初学者快速入门。

  2. 强大的生态系统:拥有丰富的库和框架,覆盖多个应用领域。

  3. 跨平台性:可在多种操作系统上运行,提高了灵活性。

  4. 广泛应用:适用于Web开发、数据分析、AI等多个领域。

  5. 活跃社区:有大量教程和支持资源,便于解决问题。

但是python的缺点也非常突出,即运行速度相对较慢,尤其是涉及到CPU密集型任务时!

因此,在本文中,我尝试用python、cpp、rust编写相同的代码并运行它,以便一次性看到这三种语言的速度。

python

python 复制代码
import time
a = time.time()
counter = 0
while (counter < 1000000000):
 counter+=1

print(counter)

b = time.time()
print("use time:%d"%(b-a))

这是一个简单的"while 循环",现在让我们看看达到 10 亿并将其打印出来所需的时间。

这是终端的实际屏幕截图,显示了打印1000,000,000 所需的全部时间。

对于Python来说,花费了83秒,将10亿并打印到终端。

cpp

cpp 复制代码
#include <iostream>
#include <chrono>

int main() {
   size_t counter = 0;
   auto start = std::chrono::high_resolution_clock::now();
   while (counter < 1000000000) {
       counter++;
  }
   auto end = std::chrono::high_resolution_clock::now();
   auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
   std::cout << "Counter: " << counter << ", Time taken: " << duration.count() << " milliseconds" << std::endl;
   return 0;
}

将以上cpp代码用g++编译为exe,并运行,结果如下:

对于cpp来说,花费了1461毫秒,将10亿并打印到终端。

rust

rust 复制代码
use std::time::Instant;

fn main() {
   let mut counter = 0;
   let start = Instant::now();
   
   while counter < 1_000_000_000 {
       counter += 1;
  }
   
   let end = Instant::now();
   let duration = end.duration_since(start);
   let duration_ns = duration.as_nanos();
   
   println!("Counter: {}, Time taken: {} nanoseconds", counter, duration_ns);
}

将以上rust代码用cargo编译为exe,并运行,结果如下:

对于rust来说,花费了100纳秒,将10亿并打印到终端。


其实这个结果也出乎我的意料,本来以为速度快慢关系是 C++ > RUST >> python。没想到实际上是 RUST >C++ >> python

这点我想不明白。难度是rust编译器优化得好?有知道的小伙伴请在留言区指导y

有一点结论是很肯定的,python作为一门胶水语言相较于其他语言来说,处理速度是非常慢。

相关推荐
朔北之忘 Clancy5 分钟前
2020 年 6 月青少年软编等考 C 语言二级真题解析
c语言·开发语言·c++·学习·青少年编程·题解·尺取法
消失的旧时光-194311 分钟前
C++ 中的 auto 与 nullptr:不是语法糖,而是类型系统升级
开发语言·c++
其美杰布-富贵-李22 分钟前
PyTorch Lightning
人工智能·pytorch·python·training
fpcc23 分钟前
跟我学C++中级篇—C++17中的元编程逻辑操作
c++·模板编程
HABuo23 分钟前
【Linux进程(五)】进程地址空间深入剖析-->虚拟地址、物理地址、逻辑地址的区分
linux·运维·服务器·c语言·c++·后端·centos
开开心心_Every24 分钟前
安卓做菜APP:家常菜谱详细步骤无广简洁
服务器·前端·python·学习·edge·django·powerpoint
SiYuanFeng24 分钟前
pytorch常用张量构造词句表和nn.组件速查表
人工智能·pytorch·python
MistaCloud24 分钟前
Pytorch深入浅出(十四)之完整的模型训练测试套路
人工智能·pytorch·python·深度学习
知乎的哥廷根数学学派25 分钟前
基于物理信息嵌入与多维度约束的深度学习地基承载力智能预测与可解释性评估算法(以模拟信号为例,Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习
古城小栈27 分钟前
Rust 丰富&好用的 格式化语法
前端·算法·rust