Skip to main content
deleted 38 characters in body
Source Link
user84118
user84118
void withFlushwithSync() {   FILE* f;  int f = fopen open( "/tmp/t7"t8" , "a+"O_RDWR | O_CREAT ); fseeklseek (f, 0, SEEK_SET ); int records = 10*1000; clock_t ustart = clock(); for(int i = 0; i < records; i++) {   fputs write(f, "012345678901234567890123456789" , f 30); if(fflushfsync(f) != 0) { printf("fflush failed!"); } } clock_t uend = clock(); fcloseclose (f); printf("With" flush %lf sync() seconds:%lf \n" writes per second:%lf\n", ((double)(uend-ustart))/(CLOCKS_PER_SEC), ((double)records)/((double)(uend-ustart))/(CLOCKS_PER_SEC)); } 
With flushsync() seconds:0.030280006268 0.000000 writes per second:0.000002 
public void testFileChannel() throws IOException { 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); c.force(false); } long e=System.currentTimeMillis(); raf.close(); System.out.println("FileChannel rw"With force=trueflush "+(e-s)); } 
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)); } 
With flush 0.030280 0.000000 
public void testFileChannel() throws IOException { 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); c.force(false); } long e=System.currentTimeMillis(); raf.close(); System.out.println("FileChannel rw force=true "+(e-s)); } 
void withSync() { int f = open( "/tmp/t8" , O_RDWR | O_CREAT ); lseek (f, 0, SEEK_SET ); int records = 10*1000; clock_t ustart = clock(); for(int i = 0; i < records; i++) { write(f, "012345678901234567890123456789" , 30); fsync(f); } clock_t uend = clock(); close (f); printf("  sync() seconds:%lf  writes per second:%lf\n", ((double)(uend-ustart))/(CLOCKS_PER_SEC), ((double)records)/((double)(uend-ustart))/(CLOCKS_PER_SEC)); } 
sync() seconds:0.006268 writes per second:0.000002 
public void testFileChannel() throws IOException { 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); c.force(false); } long e=System.currentTimeMillis(); raf.close(); System.out.println("With flush "+(e-s)); } 
deleted 968 characters in body
Source Link
user84118
user84118
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 
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 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.getChannel();   b = ByteBuffer.allocateDirect(64*1024); s = System.currentTimeMillis(); for(int i=0;i<10000;i++){ b.clear(); b.put("012345678901234567890123456789".getBytes());   b.flip(); c.write(b);  c.force(false); } e=System.currentTimeMillis();  raf.close(); } 
Witbout flush 147 With flush 4263 
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)); } 

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 
public void testFileChannel() throws IOException { 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); c.force(false); } long e=System.currentTimeMillis(); raf.close(); System.out.println("FileChannel rw force=true "+(e-s)); } 
With flush 4263 
added 149 characters in body
Source Link
user84118
user84118

Note: I am using the RandomAccessFile class in combination with a ByteBuffer as ultimately we need random read/write access on this file.

Note: I am using the RandomAccessFile class in combination with a ByteBuffer as ultimately we need random read/write access on this file.

Source Link
user84118
user84118
Loading