Rust、D、Go三者的Pk1

Iilovetopview · · 8103 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

比较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:

内存占用:

运行时长:


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:Iilovetopview

查看原文:Rust、D、Go三者的Pk1

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

8103 次点击  
加入收藏 微博
上一篇:探究golang接口
下一篇:slices _ golang
2 回复  |  直到 2000-01-01 00:00:00
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传