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
int i, file;
file = open("/tmp/1.txt", O_CREAT|O_RDWR, S_IWUSR);
From: c400-l-bounces+peter.daransky=uc4.com@xxxxxxxxxxxx [mailto:c400-l-bounces+peter.daransky=uc4.com@xxxxxxxxxxxx] On Behalf Of Boris
Sent: Dienstag, 24. Mai 2011 05:21
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:
out = fopen("fileout.txt","w");
/*out = fopen("fileout.txt","wb,type=record,lrecl=2000");*/
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 mailing list archive is Copyright 1997-2020 by midrange.com 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 on our policy page. If you have questions about this, please contact