home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.3 KB  |  183 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * Program to test different ways to get the time; right now it is tuned
  21.  * only for solaris.
  22.  *   solaris results (100000 iterations):
  23.  *          time to get time with time():   4.63 usec avg, 463 msec total
  24.  *     time to get time with gethrtime():   2.17 usec avg, 217 msec total
  25.  *  time to get time with gettimeofday():   1.25 usec avg, 125 msec total
  26.  *
  27.  *
  28.  */
  29. /***********************************************************************
  30. ** Includes
  31. ***********************************************************************/
  32. /* Used to get the command line option */
  33. #include "plgetopt.h"
  34.  
  35. #include "nspr.h"
  36. #include "prpriv.h"
  37. #include "prinrval.h"
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. #define DEFAULT_COUNT 100000
  44. PRInt32 count;
  45.  
  46. time_t itime;
  47. hrtime_t ihrtime;
  48.  
  49. void
  50. ftime_init()
  51. {
  52.    itime = time(NULL);
  53.    ihrtime = gethrtime();
  54. }
  55.  
  56. time_t
  57. ftime()
  58. {
  59.         hrtime_t now = gethrtime();
  60.  
  61.         return itime + ((now - ihrtime) / 1000000000ll);
  62. }
  63.  
  64. static void timeTime(void)
  65. {
  66.     PRInt32 index = count;
  67.     time_t rv;
  68.  
  69.     for (;index--;)
  70.         rv = time(NULL);
  71. }
  72.  
  73. static void timeGethrtime(void)
  74. {
  75.     PRInt32 index = count;
  76.     time_t rv;
  77.  
  78.     for (;index--;)
  79.         rv = ftime();
  80. }
  81.  
  82. #include <sys/time.h>
  83. static void timeGettimeofday(void)
  84. {
  85.     PRInt32 index = count;
  86.     time_t rv;
  87.     struct timeval tp;
  88.  
  89.     for (;index--;)
  90.         rv = gettimeofday(&tp);
  91. }
  92.  
  93. static void timePRTime32(void)
  94. {
  95.     PRInt32 index = count;
  96.     PRInt32 rv32;
  97.     PRTime q;
  98.     PRTime rv;
  99.  
  100.     LL_I2L(q, 1000000);
  101.  
  102.     for (;index--;) {
  103.         rv = PR_Now();
  104.         LL_DIV(rv, rv, q);
  105.         LL_L2I(rv32, rv);
  106.     }
  107. }
  108.  
  109. static void timePRTime64(void)
  110. {
  111.     PRInt32 index = count;
  112.     PRTime rv;
  113.  
  114.     for (;index--;)
  115.         rv = PR_Now();
  116. }
  117.  
  118. /************************************************************************/
  119.  
  120. static void Measure(void (*func)(void), const char *msg)
  121. {
  122.     PRIntervalTime start, stop;
  123.     double d;
  124.     PRInt32 tot;
  125.  
  126.     start = PR_IntervalNow();
  127.     (*func)();
  128.     stop = PR_IntervalNow();
  129.  
  130.     d = (double)PR_IntervalToMicroseconds(stop - start);
  131.     tot = PR_IntervalToMilliseconds(stop-start);
  132.  
  133.     if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
  134. }
  135.  
  136. void main(int argc, char **argv)
  137. {
  138.     /* The command line argument: -d is used to determine if the test is being run
  139.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  140.     All of the printfs associated with this test has been handled with a if (debug_mode)
  141.     test.
  142.     Usage: test_name -d
  143.     */
  144.     PLOptStatus os;
  145.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  146.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  147.     {
  148.         if (PL_OPT_BAD == os) continue;
  149.         switch (opt->option)
  150.         {
  151.         case 'd':  /* debug mode */
  152.             debug_mode = 1;
  153.             break;
  154.          default:
  155.             break;
  156.         }
  157.     }
  158.     PL_DestroyOptState(opt);
  159.  
  160.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  161.     PR_STDIO_INIT();
  162.  
  163.     if (argc > 1) {
  164.     count = atoi(argv[1]);
  165.     } else {
  166.     count = DEFAULT_COUNT;
  167.     }
  168.  
  169.     ftime_init();
  170.  
  171.     Measure(timeTime, "time to get time with time()");
  172.     Measure(timeGethrtime, "time to get time with gethrtime()");
  173.     Measure(timeGettimeofday, "time to get time with gettimeofday()");
  174.     Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
  175.     Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
  176.  
  177.     PR_Cleanup();
  178.     return 0;
  179. }
  180.  
  181.  
  182.  
  183.