再起呼び出しをするプログラムの実行時間を測りたかった
auです。
今回は、以前作成した再起呼び出しをして最大公約数を求めるプログラムは何回か呼び出されると時間は変わってくのかということを調べました。
結果からいうと、プログラムの処理時間が短すぎて数値に出すことができませんでした。
作成したプログラム
#include <stdio.h> #include <time.h> int g_common_divisor(int x, int y) { int rem = 0; int res = 0; long double start = 0; long double end = 0; start = clock(); res = x / y; rem = x % y; if (rem == 0) { end = clock(); printf("%Lfms\n", end - start); return y; } x = y; y = rem; end = clock(); printf("%Lfms\n", end - start); return g_common_divisor(x, y); } int main(void) { int x = 0; int y = 0; int result = 0; printf("1つ目の数字"); scanf("%d", &x); printf("2つ目の数字"); scanf("%d", &y); result = g_common_divisor(x, y); printf("%dと%dの最大公約数は%dです\n", x, y, result); return 0; } // 実行結果 1つ目の数字90 2つ目の数字38 0.000000ms 0.000000ms 0.000000ms 0.000000ms 0.000000ms 90と38の最大公約数は2です
全て0.000000msでとなり、結果を得ることはできませんでした。