卡卷网
当前位置:卡卷网 / 每日看点 / 正文

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

作者:卡卷网发布时间:2025-01-31 14:24浏览数量:85次评论数量:0次

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; } }

END

免责声明:本文由卡卷网编辑并发布,但不代表本站的观点和立场,只提供分享给大家。

卡卷网

卡卷网 主页 联系他吧

请记住:卡卷网 Www.Kajuan.Net

欢迎 发表评论:

请填写验证码