:o
/me was experimenting with the NIO package few minutes ago
/me picks up his jaw
/me leaves
:o
/me was experimenting with the NIO package few minutes ago
/me picks up his jaw
/me leaves
Which JDK/RE dist?
What were ya moving?
Our tests for our game book showed some interesting results for certain buffer sizes…
I’m using the newly released Beta, the 1.5.
I was loading and saving files of ~1mb in size and they took a little less than .01 sec each to save/load.
Very impressive in my book
Well I’ll take that back, loading ain’t .01 s for sure, it’s more likely .04 s for .5 meg file.
Still pretty decent
12 meg BMP file loaded into a Buffered image 30 times.
Used nanoTime() for time measurements
0.5513015
0.43309337
0.4326227
0.42480677
0.44826984
0.4392897
0.4407508
0.45602912
0.422791
0.41829714
0.42564127
0.46178436
0.44194937
0.4183817
0.42540553
0.41966453
0.5335501
0.4897753
0.4385598
0.43106866
0.429915
0.45493767
0.44871792
0.4765585
0.41891822
0.43527684
0.45020917
0.45965165
0.4203453
0.4176206
0.4338188
0.42069072
0.42639792
0.43123522
0.44919837
0.43107706
0.4326319
0.45190325
0.42833588
0.44125915
Mean (40 iterations) 0.4427933
With this sort of test it is NOT valid to repeat it many times and take the average. After the first load the file is likely cached in RAM by the OS anyway… But come to think of it, in this case that eliminates the disk overhead so you only test transferring the data to the java buffer… hmm maybe it is valid for something, but just know what you are measuring.
0.14044498
0.07139052
0.076452695
0.07659108
0.07640024
0.08001636
0.07646246
0.07608609
Mean (8 Iterations)0.08423056
8 different files loaded of size 17.2 mb
Here’s another example:
/**
* Program: FastCopyFile.java
* Author : Craig Mattocks
* Date : April, 2003
* Description: Test Java 1.4 NIO channels and buffers.
* Based on Listing 1.2 from:
* "JDK 1.4 Tutorial" by Gregory M. Travis, Manning
* Note: Direct buffers allocate their data directly in the
* runtime environment memory, bypassing the JVM|OS boundary,
* usually doubling file copy speed. However, they generally
* cost more to allocate.
*/
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.text.NumberFormat;
public class FastCopyFile
{
public static void main( String args[] )
{
if (args.length != 2)
{
System.out.println("Usage: java FastCopyFile src_file dest_file");
System.exit(1);
}
System.out.println("Copying file " + args[0] + " to file " + args[1] + "...");
long before = System.currentTimeMillis(); // Start timing
copyFile(args[0], outfile);
long after = System.currentTimeMillis(); // End timing
double cputime = .001 * (after - before);
NumberFormat digits = NumberFormat.getInstance();
digits.setMaximumFractionDigits(3);
System.out.println("Execution required " + digits.format(cputime) + " seconds.");
}
private static void copyFile(String infile, String outfile)
{
final boolean DIRECT = true; // Direct buffer?
final boolean TRANSFER = true; // Use fast transfer method?
final int BUFFSIZE = 1024;
ByteBuffer buffer;
FileChannel in = null;
FileChannel out = null;
try
{
in = (new FileInputStream (infile )).getChannel();
out = (new FileOutputStream(outfile)).getChannel();
if (DIRECT)
buffer = ByteBuffer.allocateDirect( BUFFSIZE );
else
buffer = ByteBuffer.allocate( BUFFSIZE );
if (TRANSFER)
{
/**
* transferTo is potentially much more efficient than a simple
* loop that reads from a source channel and writes to a target
* channel. Many operating systems can transfer bytes directly
* from the filesystem cache to the target channel without
* actually copying them by using special operating system
* features that support very fast file transfers. - Sun docs
*/
in.transferTo(0, in.size(), out);
}
else
{
int ret;
while ((ret = in.read(buffer)) > -1) // Read from input channel, write to buffer
{
buffer.flip(); // Flip buffer's read/write mode
out.write(buffer); // Read from buffer, write to output channel
buffer.clear(); // Make room for the next read
}
}
}
catch (FileNotFoundException fnfx)
{
System.err.println("File not found: " + fnfx);
}
catch (IOException iox)
{
System.err.println("I/O problems: " + iox);
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException ignored) {}
}
if (out != null)
{
try
{
out.close();
}
catch (IOException ignored) {}
}
}
}
}
Oops! Error in above FastCopyFile.java code in main():
copyFile(args[0], outfile);
should be:
copyFile(args[0], args[1]);
Sorry 'bout that!
Craig