You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

92 regels
2.5 KiB

  1. //
  2. // Lol Engine — Benchmark program
  3. //
  4. // Copyright © 2005—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <cstdio>
  16. #include <lol/engine.h>
  17. using namespace lol;
  18. static size_t const MATRIX_TABLE_SIZE = 64 * 1024;
  19. static size_t const MATRIX_RUNS = 100;
  20. void bench_matrix(int mode)
  21. {
  22. float result[5] = { 0.0f };
  23. Timer timer;
  24. /* Set up tables */
  25. mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1];
  26. float *pf = new float[MATRIX_TABLE_SIZE];
  27. for (size_t run = 0; run < MATRIX_RUNS; run++)
  28. {
  29. switch (mode)
  30. {
  31. case 1:
  32. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  33. for (int j = 0; j < 4; j++)
  34. for (int k = 0; k < 4; k++)
  35. pm[i][j][k] = rand(-2.0f, 2.0f);
  36. break;
  37. }
  38. /* Copy matrices */
  39. timer.Get();
  40. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  41. pm[i] = pm[i + 1];
  42. result[0] += timer.Get();
  43. /* Determinant */
  44. timer.Get();
  45. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  46. pf[i] = determinant(pm[i]);
  47. result[1] += timer.Get();
  48. /* Multiply matrices */
  49. timer.Get();
  50. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  51. pm[i] *= pm[i + 1];
  52. result[2] += timer.Get();
  53. /* Add matrices */
  54. timer.Get();
  55. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  56. pm[i] += pm[i + 1];
  57. result[3] += timer.Get();
  58. /* Invert matrix */
  59. timer.Get();
  60. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  61. pm[i] = inverse(pm[i]);
  62. result[4] += timer.Get();
  63. }
  64. delete[] pm;
  65. delete[] pf;
  66. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  67. result[i] *= 1e9f / (MATRIX_TABLE_SIZE * MATRIX_RUNS);
  68. msg::info(" ns/elem\n");
  69. msg::info("mat4 = mat4 %7.3f\n", result[0]);
  70. msg::info("float = mat4.det() %7.3f\n", result[1]);
  71. msg::info("mat4 *= mat4 %7.3f\n", result[2]);
  72. msg::info("mat4 += mat4 %7.3f\n", result[3]);
  73. msg::info("mat4 = mat4.invert() %7.3f\n", result[4]);
  74. }