婆罗门
精华
|
战斗力 鹅
|
回帖 0
注册时间 2008-3-23
|
楼主 |
发表于 2024-12-16 15:32
|
显示全部楼层
又找了找圆周率算法发现了很多专业选手
专业选手1 梅钦公式
- #include <iostream>
- #include <string>
- #include <chrono>
- using namespace std::chrono;
- constexpr const int32_t bit{500};
- double pi500_pro_v1()
- {
- const auto start = steady_clock::now();
- int32_t a[2] = {956, 80}, b[2] = {57121, 25}, i = 0, j, k, p, q, r, s = 2, t, u, v, N{bit}, M = 10000;
- N = N / 4 + 3;
- int32_t *pi = new int32_t[N], *e = new int32_t[N];
- while (i < N)
- pi[i++] = 0;
- while (--s + 1)
- {
- for (*e = a[k = s], i = N; --i;)
- e[i] = 0;
- for (q = 1; j = i - 1, i < N; e[i] ? 0 : ++i, q += 2, k = !k)
- for (r = v = 0; ++j < N; pi[j] += k ? u : -u)
- u = (t = v * M + (e[j] = (p = r * M + e[j]) / b[s])) / q, r = p % b[s], v = t % q;
- }
- while (--i)
- (pi[i] = (t = pi[i] + s) % M) < 0 ? pi[i] += M, s = t / M - 1 : s = t / M;
- // for (std::cout << "3."; ++i < N - 2;)
- // printf("%04id", pi[i]);
- delete[] pi, delete[] e;
- const auto end = steady_clock::now();
- duration<double, std::milli> diff = end - start;
- return diff.count();
- }
- #include <vector>
- #include <numeric>
- #include <algorithm>
- constexpr uint32_t loop{100};
- int main()
- {
- std::vector<double> results;
- results.resize(loop);
- for (uint32_t i{0}; i < loop; i++)
- results[i] = pi500_pro_v1();
- std::cout << "max cost " << *std::max_element(results.begin(), results.end()) << "ms" << '\n';
- std::cout << "min cost " << *std::min_element(results.begin(), results.end()) << "ms" << '\n';
- std::cout << "avg cost " << std::reduce(results.begin(), results.end()) / loop << "ms" << '\n';
- std::string str;
- std::getline(std::cin, str);
- return 0;
- }
复制代码 6800u
max cost 0.2177ms
min cost 0.1604ms
avg cost 0.169834ms
|
|