娱乐!娱乐!请不要诋毁任何语言!!!!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编译后执行速度惊人;
联想笔记本 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毫秒+
---------------------------
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编译的
---------------------------
Go
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毫秒+;
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多毫秒。感谢那位提示的网友。
-----------------------------------------------------------
我可不是想说谁好谁坏!!
我不是某语言拥护者。反正需要不断进步!
来源:oschina
链接:https://my.oschina.net/u/988783/blog/510932