time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * Program to test different ways to get the time; right now it is tuned
  7. * only for solaris.
  8. * solaris results (100000 iterations):
  9. * time to get time with time(): 4.63 usec avg, 463 msec total
  10. * time to get time with gethrtime(): 2.17 usec avg, 217 msec total
  11. * time to get time with gettimeofday(): 1.25 usec avg, 125 msec total
  12. *
  13. *
  14. */
  15. /***********************************************************************
  16. ** Includes
  17. ***********************************************************************/
  18. /* Used to get the command line option */
  19. #include "plgetopt.h"
  20. #include "nspr.h"
  21. #include "prpriv.h"
  22. #include "prinrval.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/time.h>
  27. #define DEFAULT_COUNT 100000
  28. PRInt32 count;
  29. time_t itime;
  30. hrtime_t ihrtime;
  31. void
  32. ftime_init()
  33. {
  34. itime = time(NULL);
  35. ihrtime = gethrtime();
  36. }
  37. time_t
  38. ftime()
  39. {
  40. hrtime_t now = gethrtime();
  41. return itime + ((now - ihrtime) / 1000000000ll);
  42. }
  43. static void timeTime(void)
  44. {
  45. PRInt32 index = count;
  46. time_t rv;
  47. for (; index--;) {
  48. rv = time(NULL);
  49. }
  50. }
  51. static void timeGethrtime(void)
  52. {
  53. PRInt32 index = count;
  54. time_t rv;
  55. for (; index--;) {
  56. rv = ftime();
  57. }
  58. }
  59. static void timeGettimeofday(void)
  60. {
  61. PRInt32 index = count;
  62. time_t rv;
  63. struct timeval tp;
  64. for (; index--;) {
  65. rv = gettimeofday(&tp, NULL);
  66. }
  67. }
  68. static void timePRTime32(void)
  69. {
  70. PRInt32 index = count;
  71. PRInt32 rv32;
  72. PRTime q;
  73. PRTime rv;
  74. LL_I2L(q, 1000000);
  75. for (; index--;) {
  76. rv = PR_Now();
  77. LL_DIV(rv, rv, q);
  78. LL_L2I(rv32, rv);
  79. }
  80. }
  81. static void timePRTime64(void)
  82. {
  83. PRInt32 index = count;
  84. PRTime rv;
  85. for (; index--;) {
  86. rv = PR_Now();
  87. }
  88. }
  89. /************************************************************************/
  90. static void Measure(void (*func)(void), const char *msg)
  91. {
  92. PRIntervalTime start, stop;
  93. double d;
  94. PRInt32 tot;
  95. start = PR_IntervalNow();
  96. (*func)();
  97. stop = PR_IntervalNow();
  98. d = (double)PR_IntervalToMicroseconds(stop - start);
  99. tot = PR_IntervalToMilliseconds(stop-start);
  100. if (debug_mode) {
  101. printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
  102. }
  103. }
  104. int main(int argc, char **argv)
  105. {
  106. /* The command line argument: -d is used to determine if the test is being run
  107. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  108. All of the printfs associated with this test has been handled with a if (debug_mode)
  109. test.
  110. Usage: test_name -d
  111. */
  112. PLOptStatus os;
  113. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  114. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  115. {
  116. if (PL_OPT_BAD == os) {
  117. continue;
  118. }
  119. switch (opt->option)
  120. {
  121. case 'd': /* debug mode */
  122. debug_mode = 1;
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. PL_DestroyOptState(opt);
  129. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  130. PR_STDIO_INIT();
  131. if (argc > 1) {
  132. count = atoi(argv[1]);
  133. } else {
  134. count = DEFAULT_COUNT;
  135. }
  136. ftime_init();
  137. Measure(timeTime, "time to get time with time()");
  138. Measure(timeGethrtime, "time to get time with gethrtime()");
  139. Measure(timeGettimeofday, "time to get time with gettimeofday()");
  140. Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
  141. Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
  142. PR_Cleanup();
  143. return 0;
  144. }