Generally I will use the POSIX API instead of STD C APIs. You can adjust it while appropriate latency but basically the Standard C (fwrite ()) API has own buffer where the data is copied and only then is written to disk.
I made ​​small tests (similar to your program) and the POSIX API was much faster as the Standard C APIs.
Try to replace the fopen () and fwrite () with open () and write() ?

My tests in (clocks):

Loop writes with 2000 max 9000 char buffer
Write STD C: 69
Write POSIX: 28

Loop 90 000 writes with 2000 char buffer
Write C STD: 687
Write POSIX: 257

void writePOSIX()
int i, file;
file = open("/tmp/1.txt", O_CREAT|O_RDWR, S_IWUSR);

memset( &buf, 'c', sizeof(buf) );
for( i = 0; i<MAX;i++ )
write( file, buf, sizeof(buf) );

close( file );

void writeSTDC()
int i = 0;
FILE *file = fopen( "/tmp/2.txt", "w" );

memset( &buf, 'c', sizeof(buf) );
for( i = 0; i<MAX;i++ )
fwrite( buf, 1, sizeof(buf), file );

fclose( file );

-----Original Message-----
From: [] On Behalf Of Boris
Sent: Dienstag, 24. Mai 2011 05:21
To: c400-l@xxxxxxxxxxxx
Subject: Re: [C400-L] C400-L Digest, Vol 9, Issue 22

Thanks for the quick response.
I'm probably missing on some basics here.
Here is a sample code:

for (i=0;i<=2000;i++)
out = fopen("fileout.txt","w");
/*out = fopen("fileout.txt","wb,type=record,lrecl=2000");*/

for (i=0;i<=900000;i++)
num = fwrite (buf, 1, sizeof(buf), out );


I removed the read function. It was populating the buf.
This program takes about 90% CPU.
I also tried:
num = fwrite (buf, sizeof(buf), 1, out ); It did reduce the CPU to about 85%. Then I increased the size of the buf to
10000 assuming it would further reduce the CPU but it instead jumped to 94%.

How do I make the program write by records/blocks? The commented line
doesn't work - fopen returns NULL.


This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available here. If you have questions about this, please contact [javascript protected email address].