ユーザ用ツール

サイト用ツール


いろんな言語でハノイの塔

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
いろんな言語でハノイの塔 [2021/04/23 15:34] – [性能] arakiいろんな言語でハノイの塔 [2021/04/30 21:38] (現在) – [Rust] araki
行 31: 行 31:
 ^bash |4.3.42 |Celeron N3150 (1.60GHz) |Vine Linux 6.5 |122 | ^bash |4.3.42 |Celeron N3150 (1.60GHz) |Vine Linux 6.5 |122 |
 ^awk (gawk) |3.1.8 |Celeron N3150 (1.60GHz) |Vine Linux 6.5 |8 | ^awk (gawk) |3.1.8 |Celeron N3150 (1.60GHz) |Vine Linux 6.5 |8 |
-^go|1.16.0|Core i3 5010U (2.1GHz) |Ubuntu 20.4 LTS|4 |+^go|1.16.0|Core i3 5010U (2.1GHz) |Ubuntu 20.4 LTS|0.004 | 
 +^Rust|1.47.0|Core i3 5010U (2.1GHz) |Ubuntu 20.4 LTS|0.004 |
 ===== プログラム ===== ===== プログラム =====
 ==== PowerShell ==== ==== PowerShell ====
行 1085: 行 1086:
 0.003u 0.005s 0:00.00 0.0%      0+0k 0+0io 0pf+0w 0.003u 0.005s 0:00.00 0.0%      0+0k 0+0io 0pf+0w
 araki@chive[158]%  araki@chive[158]% 
 +</code>
 +==== Go ====
 +=== 能書き ===
 +Goは比較的新しいコンパイラ言語である。
 +静的型付けを基本とする言語であるが、メモリ管理は言語側が行い、ガベージコレクションなどの機能も内包している。
 +
 +関数型言語であり、構造体を使った、オブジェクト指向っぽい記述もできる。
 +
 +=== コード ===
 +
 +コマンドラインの引数をとるために flagを、表示の加工用に fmtモジュールを利用している以外は、コード内で完結している。
 +
 +<code>
 +package main
 +
 +import (
 +  "fmt"
 +  "flag"
 +)
 +
 +type Discs int
 +
 +type Hanoi struct {
 +    N Discs
 +    a int
 +    b int
 +    c int
 +}
 +
 +func NewTower(n int) *Hanoi {
 +    h := new(Hanoi)
 +    h.N = Discs(n)
 +    h.a = 1
 +    h.b = 2
 +    h.c = 3
 +    return h
 +}
 +
 +func (n Discs) doit(a int, b int, c int) {
 +    if n > 0 {
 +        (n - 1).doit(a, c, b)
 +        fmt.Printf("Disc %d moved from %d to %d.\n", n, a, b)
 +        (n - 1).doit(c, b, a)
 +    }
 +}
 +
 +func (h Hanoi) run() {
 +    h.N.doit(h.a, h.b, h.c)
 +}
 +
 +func main() {
 +    n := flag.Int("n", 3, "n <NUM>: Number of Discs.")
 +    flag.Parse()
 +    hanoi := NewTower(*n)
 +    hanoi.run()
 +}
 +</code>
 +=== 実行結果 ===
 +
 +<code>
 +araki@cherry:~/work/Hanoi$ time ./hanoigo -n 4
 +Disc 1 moved from 1 to 3.
 +Disc 2 moved from 1 to 2.
 +Disc 1 moved from 3 to 2.
 +Disc 3 moved from 1 to 3.
 +Disc 1 moved from 2 to 1.
 +Disc 2 moved from 2 to 3.
 +Disc 1 moved from 1 to 3.
 +Disc 4 moved from 1 to 2.
 +Disc 1 moved from 3 to 2.
 +Disc 2 moved from 3 to 1.
 +Disc 1 moved from 2 to 1.
 +Disc 3 moved from 3 to 2.
 +Disc 1 moved from 1 to 3.
 +Disc 2 moved from 1 to 2.
 +Disc 1 moved from 3 to 2.
 +
 +real    0m0.004s
 +user    0m0.005s
 +sys     0m0.001s
 +araki@cherry:~/work/Hanoi$
 +</code>
 +
 +==== Rust ====
 +=== 能書き ===
 +システム記述言語として注目を集めているRust。
 +メモリの管理が静的に行われており、予期せぬメモリリークなどによるセキュリティホールなどを言語レベルで防止できる。
 +
 +cargoを使用してひな型を作り、getoptsによるオプション処理を加えた。
 +ハノイの塔本体部分より、オプションの処理部分のほうが大きい。
 +なお、ハノイの塔部分に関しては特にオブジェクト指向的なつくりにはしていない。
 +=== コード ===
 +コードは、RubyやPythonなどのスクリプト型言語と似た感じになっているが、静的な片付けが行われているところが、これらの言語とは違っている。
 +また、ガベージコレクションによる動的なメモリ管理ではなく、静的な管理が中心となる。
 +
 +<code>
 +use getopts::Options;
 +use std::process;
 +use std::env;
 +
 +fn print_usage(prog_name: &str, opts: Options)
 +{
 +    let brief = format!("Usage: {} [Options]", prog_name);
 +    print!("{}", opts.usage(&brief));
 +    process::exit(1);
 +}
 +
 +fn hanoi(n: i32, a: i32, b: i32, c: i32)
 +{
 +    if n > 0
 +    {
 +        hanoi(n - 1, a, c, b);
 +        println!("Disc {} moved from {} to {}", n, a, b);
 +        hanoi(n - 1, c, b, a);
 +    }
 +}
 +
 +fn main()
 +{
 +    let args: Vec<String> = env::args().collect();
 +    let program = args[0].clone();
 +
 +    let mut opts = Options::new();
 +    opts.optopt("n","discs","Set number of discs.", "NUM");
 +    opts.optflag("h","help","show this message.");
 +    let matches = match opts.parse(&args[1..])
 +    {
 +        Ok(m) => { m }
 +        Err(f) => { panic!(f.to_string()) }
 +    };
 +    if matches.opt_present("h")
 +    {
 +        print_usage(&program, opts);
 +        return;
 +    }
 +    let n = match matches.opt_get_default::<i32>("n", 3)
 +    {
 +        Ok(t) => { t }
 +        Err(f) => { panic!(f.to_string()) }
 +    };
 +    hanoi(n, 1, 2, 3);
 +}
 +</code>
 +=== 実行結果 ===
 +<code>
 +araki@cherry:~/work/Hanoi/hanoi$ time target/debug/hanoi -n 4
 +Disc 1 moved from 1 to 3
 +Disc 2 moved from 1 to 2
 +Disc 1 moved from 3 to 2
 +Disc 3 moved from 1 to 3
 +Disc 1 moved from 2 to 1
 +Disc 2 moved from 2 to 3
 +Disc 1 moved from 1 to 3
 +Disc 4 moved from 1 to 2
 +Disc 1 moved from 3 to 2
 +Disc 2 moved from 3 to 1
 +Disc 1 moved from 2 to 1
 +Disc 3 moved from 3 to 2
 +Disc 1 moved from 1 to 3
 +Disc 2 moved from 1 to 2
 +Disc 1 moved from 3 to 2
 +
 +real    0m0.004s
 +user    0m0.003s
 +sys     0m0.001s
 +araki@cherry:~/work/Hanoi/hanoi$ 
 </code> </code>
  
いろんな言語でハノイの塔.1619159663.txt.gz · 最終更新: 2021/04/23 15:34 by araki