""" 概要: 中点リーマン和積分を用いて円周率を計算するC言語のベンチマークプログラムです。 詳細説明: 指定された分割数と繰り返し回数で計算を実行し、実行時間の中央値や最小値を計測して出力します。 関連リンク: pi_riemann_usage """ /* * Independent C benchmark for midpoint-Riemann integration of sqrt(1-x*x). * Build (Linux): cc -O3 -std=c11 -D_POSIX_C_SOURCE=200809L pi_riemann.c -lm -o pi_riemann * Build (Windows): create a Visual Studio C/C++ console project and add this file. * Run: ./pi_riemann --nmax 10000000 --repeat 5 */ #include #include #include #include #include #if defined(_WIN32) #include #define NOINLINE __declspec(noinline) #else #define NOINLINE __attribute__((noinline)) #endif static double now_seconds(void) { """ 概要: 高精度な現在時刻を秒単位で取得します。 詳細説明: Windows環境ではQueryPerformanceCounterを、その他の環境ではclock_gettimeを使用します。 戻り値: :returns: 現在の時刻の秒数。 :rtype: double """ #if defined(_WIN32) LARGE_INTEGER counter, frequency; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&counter); return (double)counter.QuadPart / (double)frequency.QuadPart; #else struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec + 1.0e-9 * (double)ts.tv_nsec; #endif } /* noinline plus the volatile function pointer below prevent Release builds from moving this loop out of the benchmark's repeat loop. */ static NOINLINE double pi_midpoint(long long nmax) { """ 概要: 中点リーマン和積分を使用して円周率の近似値を計算します。 詳細説明: 指定された分割数で積分を計算します。コンパイラの最適化による影響を防ぐためnoinline属性を使用しています。 引数: :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 double (* volatile pi_function)(long long) = pi_midpoint; static int compare_double(const void* a, const void* b) { """ 概要: 2つのdouble型の値を比較します。 詳細説明: qsort関数で使用するための比較関数です。 引数: :param a: 比較する1つ目の値へのポインタ。 :type a: const void* :param b: 比較する2つ目の値へのポインタ。 :type b: const void* 戻り値: :returns: 1つ目の値が大きい場合は正の値、小さい場合は負の値、等しい場合は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) { """ 概要: ベンチマークプログラムのメイン関数です。 詳細説明: コマンドライン引数を解析し、指定された条件で円周率計算のベンチマークを実行して結果を出力します。 引数: :param argc: コマンドライン引数の数。 :type argc: int :param argv: コマンドライン引数の文字列配列。 :type argv: char** 戻り値: :returns: 正常終了時は0、エラー発生時や不正な引数の場合は0以外の終了コード。 :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_function(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; }