标签:
环境:
CPU : I5-4200
MEM: 4G
OS: 64bit -- Windows (10) & Ubuntu (trusty)
C 语言
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 bool ishuiwen(int n) { 6 int sn = 0; 7 sn = n; 8 int tn = 0; 9 while (sn != 0) { 10 tn = tn * 10 + sn % 10; 11 sn = sn / 10; 12 } 13 if (tn == n) 14 return true; 15 return false; 16 } 17 18 int hw1() { 19 int tx = 0; 20 int x = 0; 21 for (x = 0; x <= 10000000; x++) { 22 if (ishuiwen(x) == true) 23 tx ++; 24 } 25 return tx; 26 } 27 28 void runhw1() { 29 clock_t start, finish; 30 double duration; 31 start = clock(); 32 int total = hw1(); 33 finish = clock(); 34 duration = (double)(finish - start) / CLOCKS_PER_SEC; 35 printf("total = %d, %f seconds\n", total, duration); 36 } 37 int main(){ 38 runhw1(); 39 return 0; 40 }
结果:
scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ g++ t2.c scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ ./a.out total = 10999, 0.484375 seconds scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ ./a.out total = 10999, 0.437500 seconds scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ ./a.out total = 10999, 0.437500 seconds scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ g++ -O3 t2.c scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ ./a.out total = 10999, 0.156250 seconds scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ ./a.out total = 10999, 0.156250 seconds scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu$ ./a.out total = 10999, 0.140625 seconds
Go语言:
1 package main 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 func HW(num int) bool { 9 source, tnum := num, 0 10 for num != 0 { 11 tnum = tnum*10 + num%10 12 num = num / 10 13 } 14 return tnum == source 15 } 16 func hw() { 17 t1 := time.Now() 18 all, total := 10000000, 0 19 for n := 0; n <= all; n++ { 20 if HW(n) { 21 total++ 22 } 23 } 24 fmt.Printf(`total:%d time:%f`, total, time.Now().Sub(t1).Seconds()) 25 } 26 27 func main() { 28 hw() 29 }
结果:
C:\Users\whsse\ubuntu>go build go_t.go C:\Users\whsse\ubuntu>go_t.exe total:10999 time:0.216019 C:\Users\whsse\ubuntu>go_t.exe total:10999 time:0.217023 C:\Users\whsse\ubuntu>go_t.exe total:10999 time:0.214510
Ponyc语言
1 use "collections" 2 use "time" 3 4 actor Main 5 new create(env: Env) => 6 7 var all:U32 = 10000000 8 var total:U32 = 0 9 10 var t1 = Time.now() 11 for i in Range[U32](0, all) do 12 if hw(i) then total = total + 1 end 13 end 14 15 var t2 = Time.now() 16 var t = Time.wall_to_nanos(t2) - Time.wall_to_nanos(t1) 17 18 env.out.print(total.string()) 19 env.out.print((t / 1000000).string()) 20 21 22 fun hw(num: U32):Bool => 23 var num‘ = num 24 var tnum:U32 = 0 25 while num‘ != 0 do 26 tnum = (tnum * 10) + (num‘ % 10) 27 num‘ = num‘ / 10 28 end 29 if tnum == num then return true /*else false*/ end 30 false
结果:
scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu/pony_t$ ./pony_t 10999 143 scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu/pony_t$ ./pony_t 10999 149 scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu/pony_t$ ./pony_t 10999 128 scroot@DESKTOP-SA2E7TL:/mnt/c/Users/whsse/ubuntu/pony_t$ ./pony_t 10999 144
原文:http://my.oschina.net/raddleoj/blog/510932
整理于 2016-07-10
标签:
原文地址:http://www.cnblogs.com/scroot/p/5657530.html