performance of writing into a file

Hi ,
I have a question about performance of writing into a file.

Will it take more time to open a file, write a line into the file and close the file
if the file size grows bigger and bigger??

Say, I have two files of sizes 100KB and 20MB.
Will it take more time to open/write a line/close the files in case of 20MB file than 100KB file.

Thanks in advance,
-Ashish

First the open-write-close thing is fine if it happens once in a while. If it is going on constantly it is a big performance drain. There are other options - check into aio:

man aio

To answer your question: it depends on two things: your hardware and how fragmented the file is.

If the file was very recently closed then the data may still be in the on-board cache in the disk controller or SAN box cache. In which case there is no appreciable performance difference.

If the file/disk is fragmented then you have another set of problems. The first write may require an extra head seek and then some rotational latency because it is likely the filesystem will have to add another separate physical sector (cluster of sectors) to the file. Or that the write will span disjoint sectors (depending on the filesystem and what you are writing)

It also depends on how much data you are writing. write() will defer direct I/O til later under a lof of circumstances, except a fflush() or sync(). close() does not necessarily flush the file to disk - see Marc Rochkind's 'Advanced Unix Programming' for more information on deferred write. Think of write as more of a promise than a fact in some circumstances.