Back to index

lightning-sunbird  0.9+nobinonly
time.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Program to test different ways to get the time; right now it is tuned
00040  * only for solaris.
00041  *   solaris results (100000 iterations):
00042  *          time to get time with time():   4.63 usec avg, 463 msec total
00043  *     time to get time with gethrtime():   2.17 usec avg, 217 msec total
00044  *  time to get time with gettimeofday():   1.25 usec avg, 125 msec total
00045  *
00046  *
00047  */
00048 /***********************************************************************
00049 ** Includes
00050 ***********************************************************************/
00051 /* Used to get the command line option */
00052 #include "plgetopt.h"
00053 
00054 #include "nspr.h"
00055 #include "prpriv.h"
00056 #include "prinrval.h"
00057 
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 #include <string.h>
00061 #include <sys/time.h>
00062 
00063 #define DEFAULT_COUNT 100000
00064 PRInt32 count;
00065 
00066 time_t itime;
00067 hrtime_t ihrtime;
00068 
00069 void
00070 ftime_init()
00071 {
00072    itime = time(NULL);
00073    ihrtime = gethrtime();
00074 }
00075 
00076 time_t
00077 ftime()
00078 {
00079         hrtime_t now = gethrtime();
00080 
00081         return itime + ((now - ihrtime) / 1000000000ll);
00082 }
00083 
00084 static void timeTime(void)
00085 {
00086     PRInt32 index = count;
00087     time_t rv;
00088  
00089     for (;index--;)
00090         rv = time(NULL);
00091 }
00092 
00093 static void timeGethrtime(void)
00094 {
00095     PRInt32 index = count;
00096     time_t rv;
00097  
00098     for (;index--;)
00099         rv = ftime();
00100 }
00101 
00102 static void timeGettimeofday(void)
00103 {
00104     PRInt32 index = count;
00105     time_t rv;
00106     struct timeval tp;
00107  
00108     for (;index--;)
00109         rv = gettimeofday(&tp, NULL);
00110 }
00111 
00112 static void timePRTime32(void)
00113 {
00114     PRInt32 index = count;
00115     PRInt32 rv32;
00116     PRTime q;
00117     PRTime rv;
00118 
00119     LL_I2L(q, 1000000);
00120  
00121     for (;index--;) {
00122         rv = PR_Now();
00123         LL_DIV(rv, rv, q);
00124         LL_L2I(rv32, rv);
00125     }
00126 }
00127 
00128 static void timePRTime64(void)
00129 {
00130     PRInt32 index = count;
00131     PRTime rv;
00132  
00133     for (;index--;)
00134         rv = PR_Now();
00135 }
00136 
00137 /************************************************************************/
00138 
00139 static void Measure(void (*func)(void), const char *msg)
00140 {
00141     PRIntervalTime start, stop;
00142     double d;
00143     PRInt32 tot;
00144 
00145     start = PR_IntervalNow();
00146     (*func)();
00147     stop = PR_IntervalNow();
00148 
00149     d = (double)PR_IntervalToMicroseconds(stop - start);
00150     tot = PR_IntervalToMilliseconds(stop-start);
00151 
00152     if (debug_mode) printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot);
00153 }
00154 
00155 void main(int argc, char **argv)
00156 {
00157        /* The command line argument: -d is used to determine if the test is being run
00158        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00159        All of the printfs associated with this test has been handled with a if (debug_mode)
00160        test.
00161        Usage: test_name -d
00162        */
00163        PLOptStatus os;
00164        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00165        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00166     {
00167               if (PL_OPT_BAD == os) continue;
00168         switch (opt->option)
00169         {
00170         case 'd':  /* debug mode */
00171                      debug_mode = 1;
00172             break;
00173          default:
00174             break;
00175         }
00176     }
00177        PL_DestroyOptState(opt);
00178 
00179     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00180     PR_STDIO_INIT();
00181 
00182     if (argc > 1) {
00183        count = atoi(argv[1]);
00184     } else {
00185        count = DEFAULT_COUNT;
00186     }
00187 
00188     ftime_init();
00189 
00190     Measure(timeTime, "time to get time with time()");
00191     Measure(timeGethrtime, "time to get time with gethrtime()");
00192     Measure(timeGettimeofday, "time to get time with gettimeofday()");
00193     Measure(timePRTime32, "time to get time with PR_Time() (32bit)");
00194     Measure(timePRTime64, "time to get time with PR_Time() (64bit)");
00195 
00196        PR_Cleanup();
00197        return 0;
00198 }
00199 
00200 
00201