31 TProfile* p =
new TProfile(
"pFastATan2",
"fastATan2(y,x)",
n, -TMath::Pi(), TMath::Pi());
33 for (
int i = 0;
i <
n;
i++) {
34 double phi0 = -TMath::Pi() +
i * TMath::TwoPi() /
n;
35 float x = TMath::Cos(phi0);
36 float y = TMath::Sin(phi0);
37 float phi = math_utils::fastATan2(
y,
x);
38 double diff = phi - phi0;
40 diff = std::fabs(diff);
47 std::cout <<
"test fastATan2:" << std::endl;
48 std::cout <<
" Max inaccuracy " << maxDiff << std::endl;
51 std::cout <<
" Speed: " << std::endl;
54 uint32_t iterations = 10000;
57 double vxd[M], vyd[M];
61 float x = 1.e-4,
y = 1.e-3, d = 1.e-5;
62 for (
int i = 0;
i < M; ++
i,
x += d,
y += d) {
63 vx[
i] = (
i % 2) ?
x : -
x;
64 vy[
i] = (
i % 3) ?
y : -
y;
70 double scale = 1. / iterations / M;
73 auto begin = std::chrono::high_resolution_clock::now();
74 for (
size_t i = 0;
i < iterations; ++
i) {
75 for (
int j = 0;
j < M; ++
j) {
79 auto end = std::chrono::high_resolution_clock::now();
80 auto time1 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(
end - begin).count();
81 std::cout <<
" dry run: time " << time1 <<
" ns. checksum " <<
sum << std::endl;
85 begin = std::chrono::high_resolution_clock::now();
86 for (
size_t i = 0;
i < iterations; ++
i) {
87 for (
int j = 0;
j < M; ++
j) {
88 dsum += std::atan2(vyd[
j], vxd[
j]);
91 end = std::chrono::high_resolution_clock::now();
92 auto time2 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(
end - begin).count();
93 std::cout <<
" atan2(): time " << time2 <<
" ns. checksum " << dsum << std::endl;
97 begin = std::chrono::high_resolution_clock::now();
98 for (
size_t i = 0;
i < iterations; ++
i) {
99 for (
int j = 0;
j < M; ++
j) {
100 sum += atan2f(vy[
j], vx[
j]);
103 end = std::chrono::high_resolution_clock::now();
104 auto time3 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(
end - begin).count();
105 std::cout <<
" atan2f(): time " << time3 <<
" ns. checksum " <<
sum << std::endl;
109 begin = std::chrono::high_resolution_clock::now();
110 for (
size_t i = 0;
i < iterations; ++
i) {
111 for (
int j = 0;
j < M; ++
j) {
112 sum += math_utils::fastATan2(vy[
j], vx[
j]);
115 end = std::chrono::high_resolution_clock::now();
116 auto time4 = scale * std::chrono::duration_cast<std::chrono::nanoseconds>(
end - begin).count();
117 std::cout <<
" fastATan2: time " << time4 <<
" ns. checksum " <<
sum << std::endl;
118 std::cout <<
" speed up to atan2f(): " << (time3 - time1) / (time4 - time1) <<
" times " << std::endl;