benchmark.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Hermite Curve Test
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. **
  7. ** This package is free software; you can redistribute it and/or
  8. ** modify it under the terms of the GNU General Public License as
  9. ** published by the Free Software Foundation; either version 2 of
  10. ** the License, or (at your option) any later version.
  11. **
  12. ** This package is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ** General Public License for more details.
  16. **
  17. ** === N O T E S ===========================================================
  18. **
  19. ** ========================================================================= */
  20. #include <ETL/clock>
  21. #include <ETL/hermite>
  22. #include <ETL/angle>
  23. #include <ETL/fastangle>
  24. #include <ETL/fixed>
  25. #include <ETL/surface>
  26. #include <ETL/gaussian>
  27. #include <ETL/calculus>
  28. #include <stdio.h>
  29. using namespace etl;
  30. // using namespace std;
  31. #define HERMITE_TEST_ITERATIONS (100000)
  32. template <class Angle>
  33. void angle_cos_speed_test(void)
  34. {
  35. Angle a, b, c, d;
  36. float tmp, tmp2;
  37. for (tmp = -1; tmp < 1; tmp += 0.000002f) {
  38. a = (typename Angle::cos)(tmp);
  39. b = (typename Angle::cos)(tmp);
  40. c = (typename Angle::cos)(tmp);
  41. d = (typename Angle::cos)(tmp);
  42. tmp2 = ((typename Angle::cos)(a)).get();
  43. tmp2 = ((typename Angle::cos)(b)).get();
  44. tmp2 = ((typename Angle::cos)(c)).get();
  45. tmp2 = ((typename Angle::cos)(d)).get();
  46. }
  47. }
  48. template <class Angle>
  49. void angle_sin_speed_test(void)
  50. {
  51. Angle a, b, c, d;
  52. float tmp, tmp2;
  53. for (tmp = -1.0; tmp < 1.0; tmp += 0.000002f) {
  54. a = (typename Angle::sin)(tmp);
  55. b = (typename Angle::sin)(tmp);
  56. c = (typename Angle::sin)(tmp);
  57. d = (typename Angle::sin)(tmp);
  58. tmp2 = ((typename Angle::sin)(a)).get();
  59. tmp2 = ((typename Angle::sin)(b)).get();
  60. tmp2 = ((typename Angle::sin)(c)).get();
  61. tmp2 = ((typename Angle::sin)(d)).get();
  62. }
  63. }
  64. template <class Angle>
  65. void angle_tan_speed_test(void)
  66. {
  67. Angle a, b, c, d;
  68. float tmp, tmp2;
  69. for (tmp = -1; tmp < 1; tmp += 0.000002f) {
  70. a = (typename Angle::tan)(tmp);
  71. b = (typename Angle::tan)(tmp);
  72. c = (typename Angle::tan)(tmp);
  73. d = (typename Angle::tan)(tmp);
  74. tmp2 = ((typename Angle::tan)(a)).get();
  75. tmp2 = ((typename Angle::tan)(b)).get();
  76. tmp2 = ((typename Angle::tan)(c)).get();
  77. tmp2 = ((typename Angle::tan)(d)).get();
  78. }
  79. }
  80. template <class Angle, class mytan>
  81. void angle_atan2_speed_test(void)
  82. {
  83. Angle a, b, c;
  84. float x, y;
  85. for (y = -10; y < 10; y += 0.05f)
  86. for (x = -10; x < 10; x += 0.05f) {
  87. a = mytan(y, x);
  88. a = mytan(x, y);
  89. b = mytan(y, x);
  90. b = mytan(x, y);
  91. c = mytan(y, x);
  92. c = mytan(x, y);
  93. a = mytan(y, x);
  94. a = mytan(x, y);
  95. b = mytan(y, x);
  96. b = mytan(x, y);
  97. c = mytan(y, x);
  98. c = mytan(x, y);
  99. }
  100. }
  101. int fastangle_speed_test(void)
  102. {
  103. int ret = 0;
  104. float
  105. angle_cos_time,
  106. fastangle_cos_time,
  107. angle_tan_time,
  108. fastangle_tan_time,
  109. angle_atan2_time,
  110. fastangle_atan2_time,
  111. angle_sin_time,
  112. fastangle_sin_time ;
  113. etl::clock MyTimer;
  114. MyTimer.reset();
  115. angle_cos_speed_test<angle>();
  116. angle_cos_time = MyTimer();
  117. printf("angle: Cosine test: %f seconds\n", angle_cos_time);
  118. MyTimer.reset();
  119. angle_cos_speed_test<fastangle>();
  120. fastangle_cos_time = MyTimer();
  121. printf("fastangle: Cosine test: %f seconds\n", fastangle_cos_time);
  122. printf("fastangle is %.02f%% faster\n", (angle_cos_time / fastangle_cos_time) * 100.0 - 100.0);
  123. MyTimer.reset();
  124. angle_sin_speed_test<angle>();
  125. angle_sin_time = MyTimer();
  126. printf("angle: Sine test: %f seconds\n", angle_sin_time);
  127. MyTimer.reset();
  128. angle_sin_speed_test<fastangle>();
  129. fastangle_sin_time = MyTimer();
  130. printf("fastangle: Sine test: %f seconds\n", fastangle_sin_time);
  131. printf("fastangle is %.02f%% faster\n", (angle_sin_time / fastangle_sin_time) * 100.0 - 100.0);
  132. MyTimer.reset();
  133. angle_tan_speed_test<angle>();
  134. angle_tan_time = MyTimer();
  135. printf("angle: Tangent test: %f seconds\n", angle_tan_time);
  136. MyTimer.reset();
  137. angle_tan_speed_test<fastangle>();
  138. fastangle_tan_time = MyTimer();
  139. printf("fastangle: Tangent test: %f seconds\n", fastangle_tan_time);
  140. printf("fastangle is %.02f%% faster\n", (angle_tan_time / fastangle_tan_time) * 100.0 - 100.0);
  141. MyTimer.reset();
  142. angle_atan2_speed_test<angle, angle::tan>();
  143. angle_atan2_time = MyTimer();
  144. printf("angle: arcTangent2 test: %f seconds\n", angle_atan2_time);
  145. MyTimer.reset();
  146. angle_atan2_speed_test<fastangle, fastangle::tan>();
  147. fastangle_atan2_time = MyTimer();
  148. printf("fastangle: arcTangent2 test: %f seconds\n", fastangle_atan2_time);
  149. printf("fastangle is %.02f%% faster\n", (angle_atan2_time / fastangle_atan2_time) * 100.0 - 100.0);
  150. return ret;
  151. }
  152. int surface_and_gaussian_blur_test()
  153. {
  154. int ret = 0;
  155. etl::clock MyTimer;
  156. float endtime;
  157. {
  158. surface<float> my_surface(1000, 1000);
  159. MyTimer.reset();
  160. gaussian_blur(my_surface.begin(), my_surface.end(), 30, 30);
  161. endtime = MyTimer();
  162. printf("surface_and_gaussian_blur_test<float>: %f seconds\n", endtime);
  163. }
  164. {
  165. surface<double> my_surface(1000, 1000);
  166. MyTimer.reset();
  167. gaussian_blur(my_surface.begin(), my_surface.end(), 30, 30);
  168. endtime = MyTimer();
  169. printf("surface_and_gaussian_blur_test<double>: %f seconds\n", endtime);
  170. }
  171. {
  172. surface<fixed> my_surface(1000, 1000);
  173. MyTimer.reset();
  174. gaussian_blur(my_surface.begin(), my_surface.end(), 30, 30);
  175. endtime = MyTimer();
  176. printf("surface_and_gaussian_blur_test<fixed>: %f seconds\n", endtime);
  177. }
  178. return ret;
  179. }
  180. int hermite_int_test()
  181. {
  182. int ret = 0;
  183. hermite<int> Hermie;
  184. hermite<int>::time_type f;
  185. int i;
  186. etl::clock timer;
  187. etl::clock::value_type t;
  188. Hermie.p1() = 0;
  189. Hermie.t1() = 40000;
  190. Hermie.p2() = 0;
  191. Hermie.t2() = 40000;
  192. Hermie.sync();
  193. {
  194. float t;
  195. for (f = 0.0f, i = 0, timer.reset(); i < HERMITE_TEST_ITERATIONS; i++, f += 0.000005f) {
  196. t += Hermie(f) + Hermie(f + 0.1f);
  197. t += Hermie(f) + Hermie(f + 0.1f);
  198. t += Hermie(f) + Hermie(f + 0.1f);
  199. t += Hermie(f) + Hermie(f + 0.1f);
  200. t += Hermie(f) + Hermie(f + 0.1f);
  201. t += Hermie(f) + Hermie(f + 0.1f);
  202. t += Hermie(f) + Hermie(f + 0.1f);
  203. t += Hermie(f) + Hermie(f + 0.1f);
  204. t += Hermie(f) + Hermie(f + 0.1f);
  205. t += Hermie(f) + Hermie(f + 0.1f);
  206. t += Hermie(f) + Hermie(f + 0.1f);
  207. t += Hermie(f) + Hermie(f + 0.1f);
  208. }
  209. }
  210. t = timer();
  211. printf("hermite<int>:time=%f milliseconds\n", t * 1000);
  212. return ret;
  213. }
  214. int hermite_float_test(void)
  215. {
  216. int ret = 0;
  217. float f;
  218. int i;
  219. hermite<float> Hermie;
  220. etl::clock timer;
  221. double t;
  222. Hermie.p1() = 0;
  223. Hermie.t1() = 1;
  224. Hermie.p2() = 0;
  225. Hermie.t2() = 1;
  226. Hermie.sync();
  227. {
  228. float t;
  229. for (f = 0.0f, i = 0, timer.reset(); i < HERMITE_TEST_ITERATIONS; i++, f += 0.000005f) {
  230. t += Hermie(f) + Hermie(f + 0.1f);
  231. t += Hermie(f) + Hermie(f + 0.1f);
  232. t += Hermie(f) + Hermie(f + 0.1f);
  233. t += Hermie(f) + Hermie(f + 0.1f);
  234. t += Hermie(f) + Hermie(f + 0.1f);
  235. t += Hermie(f) + Hermie(f + 0.1f);
  236. t += Hermie(f) + Hermie(f + 0.1f);
  237. t += Hermie(f) + Hermie(f + 0.1f);
  238. t += Hermie(f) + Hermie(f + 0.1f);
  239. t += Hermie(f) + Hermie(f + 0.1f);
  240. t += Hermie(f) + Hermie(f + 0.1f);
  241. t += Hermie(f) + Hermie(f + 0.1f);
  242. }
  243. }
  244. t = timer();
  245. printf("hermite<float>:time=%f milliseconds\n", t * 1000);
  246. return ret;
  247. }
  248. int hermite_double_test(void)
  249. {
  250. int ret = 0, i;
  251. float f;
  252. hermite<double> Hermie;
  253. etl::clock timer;
  254. double t;
  255. Hermie.p1() = 0;
  256. Hermie.t1() = 1;
  257. Hermie.p2() = 0;
  258. Hermie.t2() = 1;
  259. Hermie.sync();
  260. for (f = 0.0f, i = 0, timer.reset(); i < HERMITE_TEST_ITERATIONS; i++, f += 0.000005f) {
  261. t += Hermie(f) + Hermie(f + 0.1f);
  262. t += Hermie(f) + Hermie(f + 0.1f);
  263. t += Hermie(f) + Hermie(f + 0.1f);
  264. t += Hermie(f) + Hermie(f + 0.1f);
  265. t += Hermie(f) + Hermie(f + 0.1f);
  266. t += Hermie(f) + Hermie(f + 0.1f);
  267. t += Hermie(f) + Hermie(f + 0.1f);
  268. t += Hermie(f) + Hermie(f + 0.1f);
  269. t += Hermie(f) + Hermie(f + 0.1f);
  270. t += Hermie(f) + Hermie(f + 0.1f);
  271. t += Hermie(f) + Hermie(f + 0.1f);
  272. t += Hermie(f) + Hermie(f + 0.1f);
  273. }
  274. t = timer();
  275. printf("hermite<double>:time=%f milliseconds\n", t * 1000);
  276. return ret;
  277. }
  278. int hermite_fixed_test(void)
  279. {
  280. int ret = 0;
  281. int i;
  282. hermite<fixed> Hermie;
  283. hermite<fixed>::time_type f;
  284. hermite<fixed>::time_type inc(0.0005f), inc2(1.10);
  285. fixed sum(0);
  286. etl::clock timer;
  287. double t;
  288. Hermie.p1() = 0;
  289. Hermie.t1() = 1;
  290. Hermie.p2() = 0;
  291. Hermie.t2() = 1;
  292. Hermie.sync();
  293. {
  294. fixed t;
  295. for (i = 0, f = 0, timer.reset(); i < HERMITE_TEST_ITERATIONS; i++, f += inc) {
  296. sum += Hermie(f) + Hermie(f + inc2);
  297. sum += Hermie(f) + Hermie(f + inc2);
  298. sum += Hermie(f) + Hermie(f + inc2);
  299. sum += Hermie(f) + Hermie(f + inc2);
  300. sum += Hermie(f) + Hermie(f + inc2);
  301. sum += Hermie(f) + Hermie(f + inc2);
  302. sum += Hermie(f) + Hermie(f + inc2);
  303. sum += Hermie(f) + Hermie(f + inc2);
  304. sum += Hermie(f) + Hermie(f + inc2);
  305. sum += Hermie(f) + Hermie(f + inc2);
  306. sum += Hermie(f) + Hermie(f + inc2);
  307. sum += Hermie(f) + Hermie(f + inc2);
  308. }
  309. }
  310. t = timer();
  311. printf("hermite<fixed>:time=%f milliseconds\n", t * 1000);
  312. return ret;
  313. }
  314. int hermite_angle_test(void)
  315. {
  316. int ret = 0, i;
  317. float f;
  318. hermite<angle> Hermie;
  319. etl::clock timer;
  320. angle tmp;
  321. double t;
  322. Hermie.p1() = angle::degrees(0);
  323. Hermie.t1() = angle::degrees(45);
  324. Hermie.p2() = angle::degrees(-45);
  325. Hermie.t2() = angle::degrees(180);
  326. Hermie.sync();
  327. for (f = 0.0f, i = 0, timer.reset(); i < HERMITE_TEST_ITERATIONS; i++, f += 0.000005f) {
  328. tmp += Hermie(f) + Hermie(f + 0.1f);
  329. tmp += Hermie(f) + Hermie(f + 0.1f);
  330. tmp += Hermie(f) + Hermie(f + 0.1f);
  331. tmp += Hermie(f) + Hermie(f + 0.1f);
  332. tmp += Hermie(f) + Hermie(f + 0.1f);
  333. tmp += Hermie(f) + Hermie(f + 0.1f);
  334. tmp += Hermie(f) + Hermie(f + 0.1f);
  335. tmp += Hermie(f) + Hermie(f + 0.1f);
  336. tmp += Hermie(f) + Hermie(f + 0.1f);
  337. tmp += Hermie(f) + Hermie(f + 0.1f);
  338. tmp += Hermie(f) + Hermie(f + 0.1f);
  339. tmp += Hermie(f) + Hermie(f + 0.1f);
  340. }
  341. t = timer();
  342. fprintf(stderr, "hermite<angle>:time=%f milliseconds\n", t * 1000);
  343. return ret;
  344. }
  345. int hermite_fastangle_test(void)
  346. {
  347. int ret = 0, i;
  348. hermite<fastangle> Hermie;
  349. hermite<fastangle>::time_type f;
  350. etl::clock timer;
  351. fastangle tmp;
  352. double t;
  353. Hermie.p1() = fastangle::degrees(0);
  354. Hermie.t1() = fastangle::degrees(45);
  355. Hermie.p2() = fastangle::degrees(-45);
  356. Hermie.t2() = fastangle::degrees(180);
  357. Hermie.sync();
  358. for (f = 0.0f, i = 0, timer.reset(); i < HERMITE_TEST_ITERATIONS; i++, f += 0.000005f) {
  359. tmp += Hermie(f) + Hermie(f + 0.1f);
  360. tmp += Hermie(f) + Hermie(f + 0.1f);
  361. tmp += Hermie(f) + Hermie(f + 0.1f);
  362. tmp += Hermie(f) + Hermie(f + 0.1f);
  363. tmp += Hermie(f) + Hermie(f + 0.1f);
  364. tmp += Hermie(f) + Hermie(f + 0.1f);
  365. tmp += Hermie(f) + Hermie(f + 0.1f);
  366. tmp += Hermie(f) + Hermie(f + 0.1f);
  367. tmp += Hermie(f) + Hermie(f + 0.1f);
  368. tmp += Hermie(f) + Hermie(f + 0.1f);
  369. tmp += Hermie(f) + Hermie(f + 0.1f);
  370. tmp += Hermie(f) + Hermie(f + 0.1f);
  371. }
  372. t = timer();
  373. fprintf(stderr, "hermite<fastangle>:time=%f milliseconds\n", t * 1000);
  374. return ret;
  375. }
  376. int main()
  377. {
  378. int error = 0;
  379. error += fastangle_speed_test();
  380. error += surface_and_gaussian_blur_test();
  381. error += hermite_float_test();
  382. error += hermite_double_test();
  383. error += hermite_int_test();
  384. error += hermite_fixed_test();
  385. error += hermite_angle_test();
  386. error += hermite_fastangle_test();
  387. return error;
  388. }