Skip to content

Instantly share code, notes, and snippets.

@keiichiw
Last active July 8, 2016 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keiichiw/e4de75fa14603b87d7cb7f7b2bbba380 to your computer and use it in GitHub Desktop.
Save keiichiw/e4de75fa14603b87d7cb7f7b2bbba380 to your computer and use it in GitHub Desktop.
Cloud 基盤ソフトウェア2016 課題2
#include <iostream>
#include <string>
#include <map>
#include <sys/time.h>
#include <gmpxx.h>
using namespace std;
const int MOD = 1000000007;
long long fib(int n) {
if (n <= 1) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
long long fact_mod_loop(int n) {
long long x = 1;
for (int i = 1; i <= n; ++i) {
x = (x * i) % MOD;
}
return x;
}
mpz_class fact_loop(int n) {
mpz_class x = 1;
for (int i = 1; i <= n; ++i) {
x = (x * i);
}
return x;
}
inline double get_dtime(void){
struct timeval tv;
gettimeofday(&tv, NULL);
return ((double)(tv.tv_sec) * 1000 + (double)(tv.tv_usec) * 0.001);
}
int main(int argc, char* argv[]) {
if (argc != 3) {
cout << "Usage: " << argv[0] << " <function> <arg>" << endl;
return 0;
}
string fun_name = string(argv[1]);
int n = stoi(string(argv[2]));
double start_time = get_dtime();
if (fun_name == "fib") {
cerr << fib(n) << endl;
} else if (fun_name == "fact_mod_loop") {
cerr << fact_mod_loop(n) << endl;
} else if (fun_name == "fact_loop") {
cerr << fact_loop(n) % ((mpz_class) MOD) << endl;
} else {
cerr << fun_name << " is not implemented!" << endl;
}
double spent = get_dtime() - start_time;
cout << spent << endl;
return 0;
}
import java.math.BigInteger;
public class Bench {
private final static long MOD = 1000000007;
public static long fib(long n) {
if (n <= 1) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
public static long factModLoop(long n) {
long x = 1;
for (long i = 1; i <= n; ++i) {
x = (x * i) % MOD;
}
return x;
}
public static BigInteger factLoop(long n) {
BigInteger x = BigInteger.ONE;
for (long i=1; i<=n; ++i) {
x = x.multiply(BigInteger.valueOf(i));
}
return x;
}
public static void main(String[] args) {
if (args.length!=2) {
System.err.println("Usage: java Bench <function> <arg>");
return;
}
String funName = args[0];
long n = Long.parseLong(args[1]);
long s_time = System.currentTimeMillis();
switch (funName) {
case "fib":
System.err.println(fib(n));
break;
case "factLoop":
System.err.println(factLoop(n).remainder(BigInteger.valueOf(MOD)));
break;
case "factModLoop":
System.err.println(factModLoop(n));
break;
default:
System.err.println(funName + " is invalid");
return;
}
long e_time = System.currentTimeMillis();
System.out.println(e_time - s_time);
}
}
#!/usr/bin/python3
import sys
import time
MOD = 1000000007
def fib(n):
if n <= 1:
return n
else:
return fib(n - 1) + fib(n - 2)
def fact_loop(n):
x = 1
for i in range(1, n + 1):
x = x * i
return x
def fact_mod_loop(n):
x = 1
for i in range(1, n + 1):
x = (x * i) % MOD
return x
def main():
argv = sys.argv
if len(argv) <= 1:
print("argv is empty")
exit(1)
funname = argv[1]
n = int(argv[2])
d = {"fib": fib, "fact_loop": fact_loop, "fact_mod_loop": fact_mod_loop}
func = d[funname]
s_time = time.time()
print(func(n) % MOD, file=sys.stderr)
e_time = time.time()
print((e_time - s_time) * 1000)
if __name__ == '__main__':
main()
all:
g++ -std=c++11 -O0 bench.cpp -o bench.out -lgmpxx -lgmp
javac Bench.java
clean:
rm -f *.out *.class *~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment