Rust、D、Go三者的Pk1
来源:互联网 发布:关口知宏日本列岛铁道 编辑:程序博客网 时间:2024/04/29 13:16
比较Rust、D、Go的单线程的运算、内存占用,Rust 1.0 alpha2(64位) ,D 2.066.1(ldc2编译器0.15.1(基于llvm3.6 msvc64))、Go 1.4.2(64位),基于win7 64位平台. 为让内存有变化,特意加入字典或关联数组.
比较2点:1、使用内存,2、运行时长。
(注:虽然此题已在 以下两篇文章中以“数学公式”法做过比较,但对运行效率的比较意义不大。
http://blog.csdn.net/iilovetopview/article/details/43745059
http://blog.csdn.net/iilovetopview/article/details/43913027
http://blog.csdn.net/iilovetopview/article/details/43960963
)
/*题目:
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。
比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么?
*/
一、用穷举法查5万以内的数,并显示出函数结果。
1、Rust:
use std::time::duration::Duration;use std::collections::HashMap;fn main() { println!("(Rust Language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:"); let r = Duration::span(fnx); println!(" time :{} seconds",r.num_seconds());} fn fnx(){let ( mut pos,mut n ,mut count,mut y)=(0,1,0,0); let mut aa: HashMap<usize, usize> = HashMap::new();while(n <= 50000){ y+=1; pos = Fx(n);aa.insert(n,pos); print!("{},",pos);if(n == pos) {count +=1;println!("n is: {0}, Fn(n) is:{1} ",n,pos);if(count >1) {break;}}if(y ==10){print!("\r\n");y =0;}n+=1;}println!("{} stop",n);}fn Fx (n: usize) -> usize {let mut total =0; for i in 0..n+1{ total += Count(i); } return total;} fn Count(n: usize) ->usize{ let (mut num,mut t) = (0,n); while(t >= 1) { if(t%10 ==1){num +=1;} t /= 10; } return num;}编译命令:rustc -O fnall5.rs
内存占用:
占用时长:
2、D:
module Fnall5;import std.stdio;import std.array;import std.conv;import std.datetime;void main(){ writeln("(D language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:");int n =1;int count,pos,y;int[int] aa;StopWatch sw;sw.start();while(n <= 50000){y++;pos = Fn(n);aa[n] = pos;write(pos,",");if(n == pos) {count++;writeln("n is: ",n, " Fn(n) is: ",pos);if(count >1) break;}if(y == 10){y = 0;writeln();}n++;}sw.stop();writeln(" time :" , sw.peek().seconds," seconds");// msecs/1000.0writeln(n," stop");}int Fn(int n){int total;for(int i=0;i<=n;i++){total += Count(i);}return total;}int Count(int n){int num;for(int t = n;t >=1;t=t/10){if(t%10 == 1) num++;}return num;}编译命令:ldc2 -m64 -O -release fnall5.d
内存占用:
占用时长:
3.Go:
package mainimport ("fmt""time")func main() {fmt.Println("(go Language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:")n := 1count :=0pos := 0 aa := make(map[int]int) y:=0t :=time.Now()for n =1;n<=50000;n++ {pos = Fn(n)aa[n] = posy++fmt.Print(pos,",")if n == pos {count++fmt.Println("n is :",n," Fn(n) is :",pos)if(count >1) {break}}if(y == 10){y =0fmt.Println()}}fmt.Println("time is :",time.Now().Sub(t).String())fmt.Println(n," stop")}func Fn(n int) int {var total intfor i:=0;i<=n;i++ {total += Count(i)}return total}func Count(n int) int{var num intfor t := n;t >=1;t=t/10 {if(t%10 == 1) {num++}}return num}编译命令:go build -ldflags "-s -w" fnall5.go
内存占用:
占用时长:
----------------------------------------------------------------------------------------------
二、穷举法查10万以内的数。
1、Rust:(此处只列出与“5万”不同的代码)
fn fnx(){let ( mut pos,mut n ,mut count)=(0,1,0); let mut aa: HashMap<usize, usize> = HashMap::new(); while(n <=100000){ pos = Fx(n);aa.insert(n,pos); if(n == pos) {count +=1;println!("n is: {0}, Fn(n) is:{1} ",n,pos);if(count >1) {break;}} n+=1;}println!("{} stop",n);}内存占用:
运行时长:
2、D:
void main(){ writeln("(D language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:");int n =1;int count,pos;int[int] aa;StopWatch sw;sw.start();while(n <= 100000){pos = Fn(n);aa[n] = pos;if(n == pos) {count++;writeln("n is: ",n, " Fn(n) is: ",pos);if(count >1) break;}n++;}sw.stop();writeln(" time :" , sw.peek().seconds," seconds");//1000.0writeln(n," stop");}
内存占用:
运行时长:
3、go:
func main() {fmt.Println("(go Language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:")n := 1count :=0pos := 0 aa := make(map[int]int) t :=time.Now()for n =1;n<=100000;n++ {pos = Fn(n)aa[n] = posif n == pos {count++fmt.Println("n is :",n," Fn(n) is :",pos)if(count >1) {break}}}fmt.Println("time is :",time.Now().Sub(t).String())fmt.Println(n," stop")}内存占用:
运行时长:
三、继续求20万以内的结果(代码与10万的基本相同,只是数值修改为200000)
1、Rust:
内存占用:
运行时长:
2、D:
内存占用:
运行时长:
3、Go:
内存占用:
运行时长:
- Rust、D、Go三者的Pk1
- Rust与D 的PK
- D语言架构师Andrei Alexandrescu谈D、Go、Rust取代C/C++
- Rust and Go
- 基于Go的D-Bus框架(三)
- 废弃文件的角斗场!!《文件大PK1.1》
- Go和Rust计算性能大比武
- (转)Rust: Rust的 Deref 运算符
- rust的一些细节
- 调试rust的宏
- 1. rust的优点
- rust的Iterator
- rust 的内存管理
- Rust的注释
- Rust 的功能函数
- Rust : 独一无二的Some
- Rust : 高富帅的match
- rust
- 《C语言及程序设计》实践参考——循环的嵌套流程图
- 14寒假集训GeoDefense
- 虚拟机安装之各类虚拟机
- Xcode 的正确打开方式——Debugging
- iptables详解
- Rust、D、Go三者的Pk1
- activity例子
- 常用链表操作(1)-链表创建
- 单例
- 安卓逆向学习笔记 (4) - 使用IDA Pro动态调试so文件
- C#: json字符串中的特殊字符处理
- [转帖]2014年中国“十大网络安全事件”盘点
- Android之获取屏幕的尺寸像素及获取状态栏标题栏高度
- 反射的深入讲解 1