当前位置:首页 > 每日看点 > 正文内容

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

卡卷网11个月前 (01-31)每日看点179

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

分享给朋友:

相关文章

电脑c盘哪些文件可以删除?

电脑c盘哪些文件可以删除?

电脑上的文件夹都是英文,很多朋友都不敢乱删,下面这几个文件夹里的文件,你可以放心删除。一、可删除的文件1、Backup这是一个备份文件夹,很多装机软件经常会把需要备份的东西,放在这个文件夹中。而当我们需要的软件正常保存之后,这些东西也就没有...

短视频如何快速变现?

短视频如何快速变现?

单人日产200条视频,一晚狂赚上千元,过程却简单到荒唐。只要你像这只猥琐的海鸥一样,做到这3点就行↓↓点开有惊喜速度快、不讲武德、不被发现那做短视频如何做到这3点?答案是养一只会叼视频的海鸥不是让你养真的海鸥而是用 ai 运营方法,类似于“...

知乎创作收益高吗?

知乎创作收益高吗?

高啊,主要是大多数人不知道怎么做。(这是以前写的,请看到最后)我之前就是傻傻写问答,文章,结果一毛钱收益没有。知乎上收益高的主要有盐选,好物推荐,致知计划,付费咨询。1、盐选吧,是写故事,小说类的天堂,还有就是大咖们的地盘了,难度高。2、好...

有没有推荐什么手游搬砖,或者是用手机就能做的工作能日入100左右就好了?

有没有推荐什么手游搬砖,或者是用手机就能做的工作能日入100左右就好了?

大家好,我是思聪。思聪游戏搬砖社每天分享真实靠谱的游戏赚钱的方法。整个游戏的攻略用一句话概括就是:打元宝兑换平台物品,xx元宝兑换一个分红物品。你把你打游戏得来的元宝去兑换平台的分红物品,就能每天领取xx元的分红。(具体看是哪个分红物品,比...

4499 元起的荣耀Magic7 系列实际使用体验怎么样,值得入手吗?

全是软文。。。。。。不过我买了,标准版。为什么买荣耀:屏幕看着舒服,玩游戏选择大屏,超声波指纹解锁。其它,系统、续航、充电、拍照,不是最强,但均衡下来短板都不是很短。其它米OV,IQ、一加,都看了,预算有限,米OV的小屏不感兴趣,OPPO硬...

都是前端框架,为什么用 React 的人会有优越感?

都是前端框架,为什么用 React 的人会有优越感?

上家公司一直搞react,最近第一次写个vue3项目。老板朋友来了看了下页面说:这用vue写的吧。我:是...老板朋友:一看就知道是vue做的。这tm你看一眼页面就知道用的啥框架?这感觉就是,我一直用苹果,偶尔用了下华为打电话,有个人离老远...

发表评论

访客

看不清,换一张

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