Now what exactly are you having a problem with ?

a) the first being faster than the second in case the file doesn't exist ?
        - the answer for that is that you're not doing error handling
          in the first program; fopen() FAILS on you and actually doesn't
          do any work, hence it's faster.
        - if you modify the second so that it doesn't pass O_CREAT, then
          it's always faster.

b) the second being faster in case the file exists ?
        - as explained, fopen() does more than open; it _MUST_ take
          longer.

Btw, the test programs as they are don't even compile. I've attached versions that do.

Best regards,
FrankH.

On Mon, 15 Jan 2007, gaurav kadyan wrote:

The two test cases

1:
int main()
{
struct timeb time1,time2;
unsigned long timeTaken;
FILE * fp;
ftime(&time1);
fp = fopen("abcd.txt","r");
timeb(&time2);
timeTaken = (1000 * (time2.time - time1.time)) + time2.millitm - time1.millitm;
printf("time taken %ld",timeTaken);
return 1;
}


2:
int main()
{
struct timeb time1,time2;
unsigned long timeTaken;
int fd;

ftime(&time1);
fd = open("abcd.txt",O_CREAT);
timeb(&time2);
timeTaken = (1000 * (time2.time - time1.time)) + time2.millitm - time1.millitm;
printf("time taken %ld",timeTaken);
return 1;
}


This message posted from opensolaris.org
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main()
{
hrtime_t t1, t2;
unsigned long timeTaken;
FILE * fp;
t1 = gethrtime();
fp = fopen("abcd.txt","r");
t2 = gethrtime();
if (fp == NULL)
        printf("fopen() failed, errno = %d\n", errno);
timeTaken = (long)(t2 - t1);
printf("time taken %ld\n",timeTaken);
return 1;                                                                       
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>

int main()
{
hrtime_t t1, t2;
unsigned long timeTaken;
int fd;
t1 = gethrtime();
fd = open("abcd.txt", 0);
t2 = gethrtime();
if (fd < 0)
        printf("open() failed, errno = %d\n", errno);
timeTaken = (long)(t2 - t1);
printf("time taken %ld\n",timeTaken);
return 1;                                                                       
}
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to