clock.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Clock 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 <stdio.h>
  22. using namespace etl;
  23. int basic_test(void)
  24. {
  25. int ret = 0;
  26. fprintf(stderr, "default etl::clock precision: %0.8f\n", etl::clock::precision());
  27. fprintf(stderr, "realtime etl::clock precision: %0.8f\n", etl::clock_realtime::precision());
  28. fprintf(stderr, "proctime etl::clock precision: %0.8f\n", etl::clock_proctime::precision());
  29. etl::clock_realtime timer;
  30. etl::clock::value_type amount, total;
  31. for (amount = 3.0; amount >= 0.00015; amount /= 2.0) {
  32. if (amount * 1000000.0 < 1000.0f) {
  33. fprintf(stderr, "waiting %f microseconds...\n", amount * 1000000.0);
  34. } else if (amount * 1000.0 < 400.0f) {
  35. fprintf(stderr, "waiting %f milliseconds...\n", amount * 1000.0);
  36. } else {
  37. fprintf(stderr, "waiting %f seconds...\n", amount);
  38. }
  39. timer.reset();
  40. etl::clock::sleep(amount);
  41. total = timer();
  42. if ((total - amount) * 1000000.0 < 1000.0f) {
  43. fprintf(stderr, " ** I waited %f seconds, error of %f microseconds\n", total, (total - amount) * 1000000);
  44. } else if ((total - amount) * 1000.0 < 400.0f) {
  45. fprintf(stderr, " ** I waited %f seconds, error of %f milliseconds\n", total, (total - amount) * 1000);
  46. } else {
  47. fprintf(stderr, " ** I waited %f seconds, error of %f seconds\n", total, total - amount);
  48. }
  49. }
  50. return ret;
  51. }
  52. int main()
  53. {
  54. int error = 0;
  55. error += basic_test();
  56. return error;
  57. }