分享
  1. 首页
  2. 文章

C,Go,Rust,Nim 4语回文数大战!仅供娱乐参考!

捍卫机密 · · 4589 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

联想笔记本 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, 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毫秒+

我可不是想说谁好谁坏!!

我不是某语言拥护者。反正需要不断进步!


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:捍卫机密

查看原文:C,Go,Rust,Nim 4语回文数大战!仅供娱乐参考!

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
4589 次点击
3 回复 | 直到 2018年10月01日 01:32:24
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏