This is a creation in Article, where the information may have evolved or changed.
Compare Rust, D, Go's single-threaded operation, memory footprint, Rust 1.0 ALPHA2 (64-bit), D-2.066.1 (LDC2 compiler 0.15.1 (based on llvm3.6 MSVC64)), go 1.4.2 (64-bit), Win7 64-bit platform. To make the memory change, specifically add a dictionary or associative array.
Compare 2 points: 1, use memory, 2, run time.
(Note: Although this problem has been in the following two articles in the "mathematical formula" method has been compared, but the efficiency of the comparison is not very significant.
http://blog.csdn.net/iilovetopview/article/details/43745059
http://blog.csdn.net/iilovetopview/article/details/43913027
http://blog.csdn.net/iilovetopview/article/details/43960963
)
/* Title:
There is an integer n, which writes a function f (n), which returns the number of "1" that occurs between 0 and N.
For example f (13) = 6, now f (1) = 1, ask the next largest f (n) =n n is what?
*/
First, use the poor lifting method to check the number within 50,000, and show the function results.
1. Rust:
Use Std::time::d uration::D uration;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 () and 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/=; } return num;}
Compile command: Rustc-o fnall5.rs
Memory consumption:
Length of Occupation:
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++;p os = 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 = = ten) {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;}
Compile command: Ldc2-m64-o-release fnall5.d
Memory consumption:
Length of Occupation:
3.Go:
Package Mainimport ("FMT" "Time") Func main () {FMT. Println ("Go Language"} 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 = = ten) {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}
Compile command: Go build-ldflags "-s-w" Fnall5.go
Memory consumption:
Length of Occupation:
----------------------------------------------------------------------------------------------
Second, the poor lifting method to check the number within 100,000.
1. Rust: (only the code that differs from "50,000" is listed here)
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);}
Memory consumption:
Operating Length:
2, D:
void Main () {Writeln ("(D Language)}} 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");}
Memory consumption:
Operating Length:
3, go:
Func Main () {FMT. Println ("Go Language"} 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")}
Memory consumption:
Operating Length:
Third, continue to seek results within 200,000 (the code is basically the same as 100,000, but the value is modified to 200000)
1. Rust:
Memory consumption:
Operating Length:
2, D:
Memory consumption:
Operating Length:
3, Go:
Memory consumption:
run time: