void withFlush() { FILE* f; f = fopen ( "/tmp/t7" , "a+" ); fseek (f, 0, SEEK_SET ); int records = 10*1000; clock_t ustart = clock(); for(int i = 0; i < records; i++) { fputs ("012345678901234567890123456789" , f ); if(fflush(f) != 0) { printf("fflush failed!"); } } clock_t uend = clock(); fclose (f); printf("With flush %lf %lf \n", ((double)(uend-ustart))/(CLOCKS_PER_SEC), ((double)records)/((double)(uend-ustart))/(CLOCKS_PER_SEC)); } void withoutFlush() { FILE* f; f = fopen ( "/tmp/t7" , "a+" ); fseek (f, 0, SEEK_SET ); int records = 10*1000; clock_t ustart = clock(); for(int i = 0; i < records; i++) { fputs("012345678901234567890123456789" , f ); } clock_t uend = clock(); fclose (f); printf("Without flush %lf %lf \n", ((double)(uend-ustart))/(CLOCKS_PER_SEC), ((double)records)/((double)(uend-ustart))/(CLOCKS_PER_SEC)); }
In the above code, 10,000 records can be written and flushed out to disk in a fraction of a second, output below:
With flush 0.030280 0.000000 Without flush 0.001293 0.000008
public void testFileChannel() throws IOException { // No flushing RandomAccessFile raf = new RandomAccessFile(new File("/tmp/t5"),"rw"); FileChannel c = raf.getChannel(); c.force(true); ByteBuffer b = ByteBuffer.allocateDirect(64*1024); long s = System.currentTimeMillis(); for(int i=0;i<10000;i++){ b.clear(); b.put("012345678901234567890123456789".getBytes()); b.flip(); c.write(b); } long e=System.currentTimeMillis(); raf.close(); System.out.println("FileChannel rw force=true "+(e-s)); // Flushing raf = new RandomAccessFile(new File("/tmp/t5"),"rw"); raf.seek(0); c = raf.getChannelforce(false); b = ByteBuffer.allocateDirect(64*1024);} s =long Systeme=System.currentTimeMillis(); for(int i=0;i<10000;i++){ b.clear(); b.put("012345678901234567890123456789"raf.getBytesclose()); bSystem.flip(); cout.writeprintln(b); "FileChannel rw force=true c.force"+(falsee-s); } e=System.currentTimeMillis(); raf.close(); }
Witbout flush 147 With flush 4263