娛樂!娛樂!請不要詆譭任何語言!!!!20151030測試了Rust 1.4;20151102測試了nim0.12;20151214測試了Rust 1.5 GCC版;20160127測試Rust 1.6 MSVC;20160127 Nim 0.13;20160131在樹莓派兼容的香蕉派m1上編譯的go1.5測試;加了個D語言(C的代碼就改了時間部分就移植了);20160307改用rust1.7(ms)重編譯了;20160416rust1.8ms編譯; 20160527Rust1.9MSVC編譯;[20160712]Rust 1.10MSVC編譯;20160829Nim改releas編譯;20171123 rust改用release編譯且換官方時間庫release編譯後執行速度驚人;bash
聯想筆記本 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毫秒+ui
---------------------------spa
DLang(在vs中用D插件編譯的)插件
import std.stdio; import std.datetime; 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; } int main(string[] argv) { auto currentTime1 = Clock.currTime(); auto all = hw1(); auto currentTime2 = Clock.currTime(); writeln("it is ", all, "have ", currentTime2 - currentTime1, " sec."); return 0; }
764毫秒左右。D 2.07編譯的code
---------------------------get
Gostring
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)) }
Go 1.5.1 200毫秒+;it
Go 1.5.2 209毫秒+;
香蕉派m1上,Go1.5,2.5秒左右;
----------------
Rust
use std::time::SystemTime; fn main() { hw21(); } fn hw21(){ let local1 = SystemTime::now(); hw2(); let local2 = SystemTime::now(); let rtime = local2.duration_since(local1); println!("{:?}", rtime); } 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; }
Rust 1.3 900毫秒+
Rust 1.4 同一套代碼,用時飆到了1100毫以上。最高的1500多毫秒!
Rust 1.5 GCC 同一套代碼,用時927毫秒! MSVC版的 Rust 1.5 沒有編譯成功!
Rust 1.6 MSVC版 959 毫秒+,數次平均970毫秒左右
Rust 1.7 MSVC版本 967毫秒,不是特別穩定,96X毫秒-1秒100毫秒不等。【20160307】
Rust 1.8 MSVC版本 95X 毫秒,比較穩定,95X毫秒-96X毫秒不等。【20160416】每次不要覆蓋安裝rust新版,卸載之前的安裝,否則編譯很容易出錯!
Rust1.9 MSVC 1秒多,比1.8退步了。可是這不表明Rust退步了。請諸位各自明辨!
Rust1.10 MSVC 1秒多。根據某個評論用戶的測試其運行大約都是220毫秒左右,因此最後執行效率可能由於各類因素而不通。
20171123 此次使用了 cargo build --release 編譯,還升級了時間計算,用的rust自帶的原生std::time。120毫秒上下。果真不負rust的稱號!
PS E:\rustprojects\rusttest> ./rust_test --10999-- Duration { secs: 1, nanos: 78146300 } // 上面是之前的 // 下面是帶有release編譯的,基本120-130毫秒之間,可是用的硬件稍有改動:i5 4460 3.2g 4核+8G+win10 F:\work\rustp\rustp02>target\release\rustp02.exe --10999-- Ok(Duration { secs: 0, nanos: 120095200 })
-----------------
Nim 0.11.2
import strutils, times proc 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 false proc hw1() : int = var tx:int = 0 for x in 0..10000000 : if ishuiwen(x) == true : tx=tx+1 return tx var t0 = times.cpuTime() var total : int = hw1() var t1 = times.cpuTime() echo("Nim HW all ok ", total, " . use : ", t1 - t0)
4000毫秒+
20151102更新了Nim 0.12版,測試迴文數計算速度有提升,2.8-2.9秒;
20160127 Nim 0.13 2.7秒多點;
20160829,在某個網友提示下加入-d:release編譯,性能提高。200多毫秒。感謝那位提示的網友。
-----------------------------------------------------------
我可不是想說誰好誰壞!!
我不是某語言擁護者。反正須要不斷進步!