utils.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef UTILS__H
  2. #define UTILS__H
  3. #include <math.h>
  4. #include <time.h>
  5. #include <iostream>
  6. using namespace std;
  7. template<class T> T CheckBounds(const T& value, const T& min, const T& max)
  8. {
  9. if (value<min) {
  10. return min;
  11. }
  12. if (value>max) {
  13. return max;
  14. }
  15. return value;
  16. };
  17. /** Teilt die Zahl num auf in zwei möglichst ähnlich große Faktoren
  18. *
  19. * @param num
  20. * @return gibt einen der beiden Faktoren zurück
  21. */
  22. inline uint SplitNumber(const uint& num)
  23. {
  24. uint factor = static_cast<uint>(floor(sqrt(num)));
  25. while (factor>1) {
  26. if ((num%factor)==0) {
  27. return factor;
  28. } else {
  29. --factor;
  30. }
  31. }
  32. return factor;
  33. }
  34. //////////////
  35. /** @brief StopWatch provides a simple means for performance tests
  36. *
  37. usage:
  38. * @code
  39. StopWatch s;
  40. DoSomething();
  41. s.stop();
  42. s.print();
  43. @endcode
  44. *
  45. * @author Frank Michler
  46. * @date 28.10.2009
  47. */
  48. class StopWatch
  49. {
  50. public:
  51. StopWatch() {cpu_start= clock();};
  52. void restart() {cpu_start= clock();};
  53. void stop() {cpu_end=clock();cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;}
  54. void print() {cout << "\n\n***********CpuTimeUsed= " << cpu_time_used << " seconds \n";}
  55. private:
  56. clock_t cpu_start, cpu_end;
  57. double cpu_time_used;
  58. };
  59. #endif // #ifndef UTILS__H