Last active
November 10, 2015 10:14
-
-
Save david50407/a80312ebc7f76516a84f to your computer and use it in GitHub Desktop.
Benchmark by doing Tower of Hanoi problem in recursive way.n = 20Pseudo code from Wikipedia: https://zh.wikipedia.org/wiki/汉诺塔
This file contains hidden or 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
/* Compile with: clang++ -Ofast */ | |
/* Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4) */ | |
/* 5.70s, 1.0Mb */ | |
#include <iostream> | |
#include <cstdio> | |
using namespace std; | |
void hannoi (int n, char from, char buffer, char to) | |
{ | |
if (n == 1) | |
{ | |
cout << "Move disk " << n << " from " << from << " to " << to << endl; | |
} | |
else | |
{ | |
hannoi (n-1, from, to, buffer); | |
cout << "Move disk " << n << " from " << from << " to " << to << endl; | |
hannoi (n-1, buffer, from, to); | |
} | |
} | |
int main() | |
{ | |
hannoi (20, 'A', 'B', 'C'); | |
return 0; | |
} |
This file contains hidden or 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
# Crystal 0.9.1 [b3b1223] (Fri Oct 30 03:48:35 UTC 2015) | |
# 4.70s, 1.3Mb | |
def hannoi(n, from, buffer, to) | |
if (n == 1) | |
puts "Move disk #{n} from #{from} to #{to}" | |
else | |
hannoi(n-1, from, to, buffer) | |
puts "Move disk #{n} from #{from} to #{to}" | |
hannoi(n-1, buffer, from, to) | |
end | |
end | |
hannoi(20, 'A', 'B', 'C') |
This file contains hidden or 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
// go version go1.2.1 linux/amd64 | |
// 3.73s, 0.7Mb | |
// notice that this program takes two cores of CPU to excute | |
package main | |
import "fmt" | |
func hannoi(n int, from rune, buffer rune, to rune) { | |
if n == 1 { | |
fmt.Printf("Move disk %d from %c to %c\n", n, from, to) | |
} else { | |
hannoi(n-1, from, to, buffer) | |
fmt.Printf("Move disk %d from %c to %c\n", n, from, to) | |
hannoi(n-1, buffer, from, to) | |
} | |
} | |
func main() { | |
hannoi(20, 'A', 'B', 'C') | |
} | |
This file contains hidden or 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
# ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux] | |
# 6.52s, 7.0Mb | |
def hannoi(n, from, buffer, to) | |
if (n == 1) | |
puts "Move disk #{n} from #{from} to #{to}" | |
else | |
hannoi(n-1, from, to, buffer) | |
puts "Move disk #{n} from #{from} to #{to}" | |
hannoi(n-1, buffer, from, to) | |
end | |
end | |
hannoi(20, 'A', 'B', 'C') |
This file contains hidden or 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
#!/usr/bin/env ruby | |
# Usage: ./xtime.rb commands | |
def mem(pid); `ps p #{pid} -o rss`.split.last.to_i; end | |
t = Time.now | |
pid = Process.spawn(*ARGV.to_a) | |
mm = 0 | |
Thread.new do | |
mm = mem(pid) | |
while true | |
sleep 0.3 | |
m = mem(pid) | |
mm = m if m > mm | |
end | |
end | |
Process.waitall | |
STDERR.puts "%.2fs, %.1fMb" % [Time.now - t, mm / 1024.0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment