Created
November 5, 2014 03:38
-
-
Save tana/87ed4c79e7d6a911566a to your computer and use it in GitHub Desktop.
ifとmatchで階乗を書いて、実行時間を比べてみる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate time; | |
fn factorial(n: uint) -> uint { | |
if n > 1 { | |
n * factorial(n - 1) | |
} else { | |
n | |
} | |
} | |
fn factorial_match(n: uint) -> uint { | |
match n { | |
0 | 1 => n, | |
_ => n * factorial(n - 1) | |
} | |
} | |
fn main() { | |
loop { | |
let if_start = time::get_time(); | |
for _ in range(0, 10000u) { | |
for _ in range(0, 1000u) { | |
let _ = factorial(10); | |
} | |
} | |
let if_end = time::get_time(); | |
println!("if {}", if_end - if_start); | |
let match_start = time::get_time(); | |
for _ in range(0, 10000u) { | |
for _ in range(0, 1000u) { | |
let _ = factorial_match(10); | |
} | |
} | |
let match_end = time::get_time(); | |
println!("match {}", match_end - match_start); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ああああ・・・
完全に間違ってました