1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * Program to test different ways to get the time; right now it is tuned
41 * solaris results (100000 iterations):
42 * time to get time with time(): 4.63 usec avg, 463 msec total
43 * time to get time with gethrtime(): 2.17 usec avg, 217 msec total
44 * time to get time with gettimeofday(): 1.25 usec avg, 125 msec total
48 /***********************************************************************
50 ***********************************************************************/
51 /* Used to get the command line option */
63 #define DEFAULT_COUNT 100000
73 ihrtime
= gethrtime();
79 hrtime_t now
= gethrtime();
81 return itime
+ ((now
- ihrtime
) / 1000000000ll);
84 static void timeTime(void)
86 PRInt32 index
= count
;
93 static void timeGethrtime(void)
95 PRInt32 index
= count
;
102 static void timeGettimeofday(void)
104 PRInt32 index
= count
;
109 rv
= gettimeofday(&tp
, NULL
);
112 static void timePRTime32(void)
114 PRInt32 index
= count
;
128 static void timePRTime64(void)
130 PRInt32 index
= count
;
137 /************************************************************************/
139 static void Measure(void (*func
)(void), const char *msg
)
141 PRIntervalTime start
, stop
;
145 start
= PR_IntervalNow();
147 stop
= PR_IntervalNow();
149 d
= (double)PR_IntervalToMicroseconds(stop
- start
);
150 tot
= PR_IntervalToMilliseconds(stop
-start
);
152 if (debug_mode
) printf("%40s: %6.2f usec avg, %d msec total\n", msg
, d
/ count
, tot
);
155 void main(int argc
, char **argv
)
157 /* The command line argument: -d is used to determine if the test is being run
158 in debug mode. The regress tool requires only one line output:PASS or FAIL.
159 All of the printfs associated with this test has been handled with a if (debug_mode)
164 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
165 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
167 if (PL_OPT_BAD
== os
) continue;
170 case 'd': /* debug mode */
177 PL_DestroyOptState(opt
);
179 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
183 count
= atoi(argv
[1]);
185 count
= DEFAULT_COUNT
;
190 Measure(timeTime
, "time to get time with time()");
191 Measure(timeGethrtime
, "time to get time with gethrtime()");
192 Measure(timeGettimeofday
, "time to get time with gettimeofday()");
193 Measure(timePRTime32
, "time to get time with PR_Time() (32bit)");
194 Measure(timePRTime64
, "time to get time with PR_Time() (64bit)");