当前位置:首页 > 每日看点

Golang与Rust哪个语言会是今后的主流?

卡卷网2年前 (2025-01-31)每日看点268

rust就算了,一个计算圆周率的程序能比py还慢,注意,这个py是官方的cpython,两者代码几乎一模一样,Python也没有搞什么黑科技之类的,rust是默认的编译参数,我实在是想不出来你一个静态的,编译到汇编的,能比跑在虚拟机上的动态语言Python还慢

==========评论区要看代码,先把连接放一下吧

为什么Rust会比Python更慢? - 知乎

为什么Rust会比Python更慢?

use rand::Rng; use std::time::Instant; fn estimate_pi(n: u32) -> f64 { let mut num_points_in_circle = 0; let mut num_points_total = 0; let mut rng = rand::thread_rng(); for _ in 0..n { let x: f64 = rng.gen(); let y: f64 = rng.gen(); // let distance = x.powi(2) + y.powi(2); let distance = x * x + y * y; if distance <= 1.0 { num_points_in_circle += 1; } num_points_total += 1; } return 4.0 * (num_points_in_circle as f64) / (num_points_total as f64); } fn main() { const N: u32 = 1_0000_0000 / 2; let start_time = Instant::now(); let pi_estimate = estimate_pi(N); let elapsed_time = start_time.elapsed(); println!("N={}, estimated value of pi: {}", N, pi_estimate); println!("耗时:{:?}", elapsed_time); }


import random import time def estimate_pi(n): num_points_in_circle = 0 num_points_total = 0 rng = random.Random() for _ in range(n): x = rng.random() y = rng.random() distance = x**2 + y**2 if distance <=1.0: num_points_in_circle+=1 num_points_total +=1 return 4.0 * (num_points_in_circle/num_points_total) def main(): n = int(1_0000_0000 / 2) start_time = time.time() pi_estimate = estimate_pi(n) elapsed_time = time.time() - start_time print(f"n={n}, estimated value of pi: {pi_estimate}") print(f"耗时:{elapsed_time}") if __name__ == "__main__": main()

在来个go的

func main() { n := uint32(1_0000_0000 / 2) start_time := time.Now() pi_estimate := estimate_pi(n) elapsed_time := time.Since(start_time) fmt.Printf("n=%v, estimated value of pi: %v\n", n, pi_estimate) fmt.Printf("耗时:%v\n", elapsed_time) } func estimate_pi(n uint32) float64 { num_points_in_circle := 0 num_points_total := 0 rng := rand.New(rand.NewSource(time.Now().UnixMilli())) for range n { x := rng.Float64() y := rng.Float64() distance := x*x + y*y if distance <= 1.0 { num_points_in_circle += 1 } num_points_total += 1 } return 4.0 * (float64(num_points_in_circle) / float64(num_points_total)) }

运行了3次:

go耗时基本在600ms左右,运行命令go run main.go

rust耗时在78-80s左右,运行命令cargo run,加上--release,基本上会稳定在550ms左右

py耗时在35-40s左右,运行命令python main.py

为什么N要除以2,和原链接的不一样,因为rust实在太慢了

===把java和C#的也补充下

java耗时在1470-1500ms左右,java就用的idea运行的,默认配置

c#耗时在780-800ms左右, 运行命令dotnet run --configuration Release

public static void main(String[] args) { for (int i = 0; i < 3; i++) { final long n = 1_0000_0000 / 2; long start = System.currentTimeMillis(); double pi_estimate = estimate_pi(n); long end = System.currentTimeMillis(); System.out.println("n="+n+" estimated value of pi:"+pi_estimate); System.out.println("耗时"+(end-start)+"ms"); } } public static double estimate_pi(long n){ int num_points_in_circle = 0; int num_points_total = 0; Random rng = new Random(); for (long i = 0; i < n; i++) { double x = rng.nextDouble(); double y = rng.nextDouble(); double distance = x*x+y*y; if (distance<=1.0) { num_points_in_circle+=1; } num_points_total+=1; } return 4.0 * (double) num_points_in_circle / (double) num_points_total; }


using System.Diagnostics; static class Program { static void Main() { const long n = 1_0000_0000 / 2; var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 3; i++) { sw.Restart(); double pi_estimate = estimate_pi(n); sw.Stop(); System.Console.WriteLine($"n={n}, estimated value of pi: {pi_estimate}"); System.Console.WriteLine($"耗时:{sw.ElapsedMilliseconds}ms"); } } static double estimate_pi(long n) { int num_points_in_circle = 0; int num_points_total = 0; Random rng = new Random(); for (long i = 0; i < n; i++) { double x = rng.NextDouble(); double y = rng.NextDouble(); double distance = x * x + y * y; if (distance <= 1.0) { num_points_in_circle += 1; } num_points_total += 1; } return 4.0 * (double)num_points_in_circle / (double)num_points_total; } }

扫描二维码推送至手机访问。

版权声明:本文由卡卷网发布,如需转载请注明出处。

本文链接:https://www.kajuan.net/ttnews/2025/01/10371.html

分享给朋友:

相关文章

电视参数哪个最重要?

电视参数哪个最重要?

在选择智能电视时,面板类型是一个关键因素。 IPS面板以其宽广的视角和出色的色彩还原能力受到青睐,尤其适合日常观看和游戏。 尽管可能会有轻微的漏光,但在大多数情况下,这并不影响使用体验。 相比之下,VA面板提供更高的对比度和更深的黑色,增强…

你手机里有哪些堪称神器的 App?

你手机里有哪些堪称神器的 App?

嗨嗨,我是赢酱~ 首先我认为对于我们有用的提升自我的都是手机里的神器 App。 一个人应该什么样子呢?在我从女孩子的角度看来,自律,上进,会打扮,情商高,会挣钱,独立,落落大方,这才是应该有的样子,我们可以借助手机不断学习改变 今天给大家准…

k70和红米note14pro+咋选啊?

k70和红米note14pro+咋选啊?

首选红米K70,可满足题主的功能需求。 红米 Note 14 pro+的使用者定位并不是学生,红米K70的使用者定位才是学生。 一、k70和红米note14pro+各自的参数 红米K70推出的定位初衷,是主打性能为主。最佳使用对象是喜欢…

中国芯片产量达1399亿颗,这意味着什么?

美国并不是没有明白人,只是特朗普不懂芯片产业的情况,冒冒失失在ZZ正确下,开启了对中国芯片的掐脖子,结果没掐死,反而让人练出了铁肺。 芯片是所有科技产业的上游,美国原本在上游呆得很舒服,靠英伟达、AMD、德州仪器、高通等这些几十年霸主地位的…

提升自己最快的方式是什么?

提升自己最快的方式是什么?

1. 稻盛和夫说过: “改变自己最快的方法就是做自己害怕的事,不敢做的事,认为自己做不到,觉得不可能的事。如果在自己的舒适区待久了,就会丧失斗志,如果想快速的改变,可以坚持去做一些对自己有益的事。 2. 早睡早起,坚持运动 保持旺盛的精力,…

如何看待台积电,三星相继停供大陆7nm及更先进芯片?

在这个事出来之前,我就看到过一个说法,两家Foundry可以在中东建厂,让中东的Fab去干“脏活”。本质上是国内企业搞几个中东的代理人,装作是中东的初创Fabless企业去下单,人家Foundry大概率睁一只眼闭一只眼,只管数钱。然而,紧接…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。