[LCP]Which one is faster - program with unix system calls or direct c code

Bill Rausch bill at numerical.com
Wed Jul 18 01:51:04 UTC 2001


At 10:35 PM -0500 7/16/01, Matthew Vanecek wrote:
>On 16 Jul 2001 11:35:49 -0700, Bill Rausch wrote:
>>  At 12:16 PM -0500 7/15/01, Matthew Vanecek wrote:
>>  >Unix system calls may be a smidge quicker wrt execution time.  However,
>>  >they are a pain to use in many cases, and in most cases, you're not
>>
>>  System calls are generally slower, not faster. To perform a system
>>  call, a context switch has to take place for the kernel to do it's
>>  thing.  The standard library routines use buffering specifically to
>>  avoid making so many system calls.
>>
>
>How about if you are doing atomic reads/writes, and using fread/fwrite
>you would be writing the same number of bytes?  If I read(fd,buf,100),
>or fread(buf,100,1,file), which would be faster?  Either way, I'm
>reading into buf, but with read(), I'm bypassing all the buffering logic
>of fread().  At least, that's the way it looks to me.

Suppose you put that into a loop to read a 100,000 byte file.  Using 
the read() approach you will hit the kernel 1000 times. Using the 
fread approach, it might actually call read() using an internal 
buffer of 16384 for example and therefore would only hit the kernel 7 
times.

If you're just reading the 100 bytes once and that's it, yes read() 
by itself might be slightly quicker.  If you use read() and really 
large buffers that you allocate, it might be slightly quicker.  If 
you are doing random access within a large file (lseek(), fseek(), 
etc.) then the raw routines might be better.

For the general case though, using the standard buffered routines is better.

-- 
  Bill Rausch, Software Development, Unix, Mac, Windows
  Numerical Applications, Inc.  509-943-0861   bill at numerical.com



More information about the linuxCprogramming mailing list