標籤:
聯想筆記本 inter i7,2.4GHz,16G,win10
C語言(應該是全C,vs2015編譯)
#include<stdio.h>#include<stdlib.h>#include<time.h>bool ishuiwen(int n) { int sn = 0; sn = n; int tn = 0; while (sn != 0) { tn = tn * 10 + sn % 10; sn = sn / 10; } if (tn == n) return true; return false;}int hw1() { int tx = 0; int x = 0; for (x = 0; x <= 10000000; x++) { if (ishuiwen(x) == true) tx ++; } return tx;}void runhw() { clock_t start, finish; double duration; start = clock(); int total = hw1(); finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("total = %d, %f seconds\n", total, duration);}
1100毫秒+
---------------------------
Go 1.5.1
func HW(num int) bool { var source int = num var tnum int = 0 for num != 0 { tnum = tnum*10 + num%10 num = num / 10 } if tnum == source { //fmt.Println(source) return true } return false}func hw() { all := 10000000 t1 := time.Now() total := 0 for n := 0; n <= all; n++ { if HW(n) { total++ } } t2 := time.Now() fmt.Println(total) fmt.Println(t2.Sub(t1))}
200毫秒+
----------------
Rust 1.2
use time::*;fn main() { hw21();}fn hw21(){ let local1 = time::now(); hw2(); let local2 = time::now(); println!("{:?}", local2-local1);}fn hw2(){ let mut tx:i32 = 0; for x in 0..10000000 { if hw(x) == true { tx=tx+1; } } println!("--{:?}--", tx);}fn hw(n: i32) -> bool { let mut sn:i32 = n; let mut tn:i32 = 0; while sn != 0 { tn = tn*10 + sn%10; sn = sn/10; } if tn == n { return true; } return false;}
900毫秒+
-----------------
Nim 0.11.2
import strutils, timesproc ishuiwen(n : int): bool = var sn : int sn = n var tn : int tn = 0 while sn != 0 : tn = tn * 10 + sn mod 10 sn = sn div 10 if tn == n : return true return falseproc hw1() : int = var tx:int = 0 for x in 0..10000000 : if ishuiwen(x) == true : tx=tx+1 return txvar t0 = times.cpuTime()var total : int = hw1()var t1 = times.cpuTime()echo("Nim HW all ok ", total, " . use : ", t1 - t0)
4000毫秒+
我可不是想說誰好誰壞!!
我不是某語言擁護者。反正需要不斷進步!
C,Go,Rust,Nim 4語迴文數大戰!僅供娛樂參考!