/* * Independent C benchmark for midpoint-Riemann integration of sqrt(1-x*x). * Build: cc -O3 -std=c11 -D_POSIX_C_SOURCE=200809L pi_riemann.c -lm -o pi_riemann * Windows (MSVC): cl /O2 /std:c11 pi_riemann.c /Fe:pi_riemann.exe * Run: ./pi_riemann --nmax 10000000 --repeat 5 */ /* 概要: 中点リーマン和による円周率計算の独立したC言語ベンチマークプログラムです。 詳細説明: 関数 sqrt(1 - x*x) を用いて円周率を近似計算し、その実行時間を計測します。 関連リンク: pi_riemann_usage */ #ifndef _WIN32 #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L #endif #endif #ifdef _WIN32 #include #endif #include #include #include #include #include static double now_seconds(void) { /* 概要: 現在時刻を秒単位で取得します。 詳細説明: Windows環境では QueryPerformanceCounter を使用し、その他のPOSIX環境では clock_gettime を使用して高分解能な時刻を取得します。 タイマーが利用できない場合はエラーを出力し、プログラムを終了します。 戻り値: :returns: 基準時からの経過秒数。 :rtype: double */ #ifdef _WIN32 LARGE_INTEGER counter, frequency; if (!QueryPerformanceFrequency(&frequency) || !QueryPerformanceCounter(&counter)) { fprintf(stderr, "High-resolution timer unavailable.\n"); exit(EXIT_FAILURE); } return (double)counter.QuadPart / (double)frequency.QuadPart; #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { perror("clock_gettime"); exit(EXIT_FAILURE); } return (double)ts.tv_sec + 1.0e-9 * (double)ts.tv_nsec; #endif } static double pi_midpoint(long long nmax) { /* 概要: 中点リーマン和を用いて円周率を計算します。 詳細説明: 関数 sqrt(1.0 - x * x) を区間 0 から 1 まで積分し、4倍することで円周率を求めます。 引数: :param nmax: 積分の分割数。 :type nmax: long long 戻り値: :returns: 計算された円周率の近似値。 :rtype: double */ const double dx = 1.0 / (double)nmax; double total = 0.0; for (long long i = 0; i < nmax; ++i) { const double x = ((double)i + 0.5) * dx; total += sqrt(1.0 - x * x); } return 4.0 * dx * total; } static int compare_double(const void *a, const void *b) { /* 概要: qsort用のdouble型比較関数です。 詳細説明: 2つのdouble型ポインタを受け取り、その値の大小関係を比較します。 昇順のソートに使用されます。 引数: :param a: 比較する1つ目の値へのポインタ。 :type a: const void * :param b: 比較する2つ目の値へのポインタ。 :type b: const void * 戻り値: :returns: aがbより大きい場合は正の数、小さい場合は負の数、等しい場合は0。 :rtype: int */ const double da = *(const double *)a; const double db = *(const double *)b; return (da > db) - (da < db); } static void usage(const char *program) { /* 概要: プログラムの使用方法を標準エラー出力に表示します。 引数: :param program: 実行時のプログラム名。 :type program: const char * */ fprintf(stderr, "Usage: %s [--nmax positive-integer] [--repeat positive-integer]\n", program); } int main(int argc, char **argv) { /* 概要: プログラムのエントリポイントです。 詳細説明: コマンドライン引数を解析して nmax と repeat を設定し、 指定された回数だけ pi_midpoint 関数を呼び出して円周率を計算します。 各実行の所要時間を計測し、中央値などの統計情報とともに結果を標準出力に表示します。 引数: :param argc: コマンドライン引数の数。 :type argc: int :param argv: コマンドライン引数の文字列配列。 :type argv: char ** 戻り値: :returns: 正常終了時は0、引数エラー時は2、メモリ確保エラー時は1。 :rtype: int */ long long nmax = 1000000; int repeat = 3; for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--nmax") == 0 && i + 1 < argc) { nmax = strtoll(argv[++i], NULL, 10); } else if (strcmp(argv[i], "--repeat") == 0 && i + 1 < argc) { repeat = (int)strtol(argv[++i], NULL, 10); } else if (strcmp(argv[i], "--help") == 0) { usage(argv[0]); return 0; } else { usage(argv[0]); return 2; } } if (nmax < 1 || repeat < 1) { usage(argv[0]); return 2; } double *times = malloc((size_t)repeat * sizeof(*times)); if (times == NULL) { fprintf(stderr, "Unable to allocate timing array.\n"); return 1; } double value = 0.0; for (int r = 0; r < repeat; ++r) { const double t0 = now_seconds(); value = pi_midpoint(nmax); times[r] = now_seconds() - t0; } qsort(times, (size_t)repeat, sizeof(*times), compare_double); const double median = (repeat % 2) ? times[repeat / 2] : 0.5 * (times[repeat / 2 - 1] + times[repeat / 2]); printf("Midpoint Riemann sum: nmax=%lld, repeat=%d\n", nmax, repeat); printf("C pi=%.15f abs.error=%.3e median=%.6f s min=%.6f s\n", value, fabs(value - acos(-1.0)), median, times[0]); free(times); return 0; }