Rust、D、Go三者的Pk1

比较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.0
	writeln(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 main
import ("fmt"
"time"
)

func main() {
	fmt.Println("(go Language)  Please wait for some minutes,will found Next Fn(n) = n ,the n is:")
	n := 1
	count :=0
	pos := 0
	 aa := make(map[int]int)
 	y:=0
	t :=time.Now()
	for n =1;n<=50000;n++ {
		pos = Fn(n)
		aa[n] = pos
		y++
		fmt.Print(pos,",")
		if n == pos {
			count++
			fmt.Println("n is :",n," Fn(n) is :",pos)
			if(count >1) {break}
		}
		if(y == 10){
			y =0
			fmt.Println()
		}

	}
	fmt.Println("time is :",time.Now().Sub(t).String())
	fmt.Println(n," stop")
}

func Fn(n int) int {
	var total int
	for i:=0;i<=n;i++ {
		total += Count(i)
	}
	return total
}

func Count(n int) int{
	var num int
	for 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.0
	writeln(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 := 1
	count :=0
	pos := 0
	 aa := make(map[int]int)
 
	t :=time.Now()
	for n =1;n<=100000;n++ {
		pos = Fn(n)
		aa[n] = pos

		if 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:

内存占用:

运行时长:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值