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/. */
7 * Program to test different ways to get the time; right now it is tuned
9 * solaris results (100000 iterations):
10 * time to get time with time(): 4.63 usec avg, 463 msec total
11 * time to get time with gethrtime(): 2.17 usec avg, 217 msec total
12 * time to get time with gettimeofday(): 1.25 usec avg, 125 msec total
16 /***********************************************************************
18 ***********************************************************************/
19 /* Used to get the command line option */
31 #define DEFAULT_COUNT 100000
41 ihrtime
= gethrtime();
47 hrtime_t now
= gethrtime();
49 return itime
+ ((now
- ihrtime
) / 1000000000ll);
52 static void timeTime(void)
54 PRInt32 index
= count
;
61 static void timeGethrtime(void)
63 PRInt32 index
= count
;
70 static void timeGettimeofday(void)
72 PRInt32 index
= count
;
77 rv
= gettimeofday(&tp
, NULL
);
80 static void timePRTime32(void)
82 PRInt32 index
= count
;
96 static void timePRTime64(void)
98 PRInt32 index
= count
;
105 /************************************************************************/
107 static void Measure(void (*func
)(void), const char *msg
)
109 PRIntervalTime start
, stop
;
113 start
= PR_IntervalNow();
115 stop
= PR_IntervalNow();
117 d
= (double)PR_IntervalToMicroseconds(stop
- start
);
118 tot
= PR_IntervalToMilliseconds(stop
-start
);
120 if (debug_mode
) printf("%40s: %6.2f usec avg, %d msec total\n", msg
, d
/ count
, tot
);
123 int main(int argc
, char **argv
)
125 /* The command line argument: -d is used to determine if the test is being run
126 in debug mode. The regress tool requires only one line output:PASS or FAIL.
127 All of the printfs associated with this test has been handled with a if (debug_mode)
132 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
133 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
135 if (PL_OPT_BAD
== os
) continue;
138 case 'd': /* debug mode */
145 PL_DestroyOptState(opt
);
147 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
151 count
= atoi(argv
[1]);
153 count
= DEFAULT_COUNT
;
158 Measure(timeTime
, "time to get time with time()");
159 Measure(timeGethrtime
, "time to get time with gethrtime()");
160 Measure(timeGettimeofday
, "time to get time with gettimeofday()");
161 Measure(timePRTime32
, "time to get time with PR_Time() (32bit)");
162 Measure(timePRTime64
, "time to get time with PR_Time() (64bit)");