stat.c (1685B)
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 /* 7 * Program to test different ways to get file info; right now it 8 * only works for solaris and OS/2. 9 * 10 */ 11 #include "nspr.h" 12 #include "prpriv.h" 13 #include "prinrval.h" 14 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <sys/stat.h> 19 20 #define DEFAULT_COUNT 100000 21 PRInt32 count; 22 23 #ifndef XP_PC 24 char* filename = "/etc/passwd"; 25 #else 26 char* filename = "..\\stat.c"; 27 #endif 28 29 static void statPRStat(void) { 30 PRFileInfo finfo; 31 PRInt32 index = count; 32 33 for (; index--;) { 34 PR_GetFileInfo(filename, &finfo); 35 } 36 } 37 38 static void statStat(void) { 39 struct stat finfo; 40 PRInt32 index = count; 41 42 for (; index--;) { 43 stat(filename, &finfo); 44 } 45 } 46 47 /************************************************************************/ 48 49 static void Measure(void (*func)(void), const char* msg) { 50 PRIntervalTime start, stop; 51 double d; 52 PRInt32 tot; 53 54 start = PR_IntervalNow(); 55 (*func)(); 56 stop = PR_IntervalNow(); 57 58 d = (double)PR_IntervalToMicroseconds(stop - start); 59 tot = PR_IntervalToMilliseconds(stop - start); 60 61 printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot); 62 } 63 64 int main(int argc, char** argv) { 65 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 66 67 if (argc > 1) { 68 count = atoi(argv[1]); 69 } else { 70 count = DEFAULT_COUNT; 71 } 72 73 Measure(statPRStat, "time to call PR_GetFileInfo()"); 74 Measure(statStat, "time to call stat()"); 75 76 PR_Cleanup(); 77 }