[EMAIL PROTECTED] wrote:
V tomto konkretnim pripade navic jeste je dobre pouzit NIO kvuli vykonu (je to obrovsky rozdil), ale hlavni je funkcnost.
Zkusil jsem si ted porovnat klasicke IO a NIO a vychazi mi, ze NIO
je pomalejsi:
import java.io.*;
import java.nio.channels.*;
public class Copy {
static final int BUFSIZE = 1024*64;
public static void main(String args[]) throws IOException {
if(args.length!=2) {
System.err.println("Usage: java Copy <src_file> <dst_file>");
System.exit(1);
}
long t1 = System.currentTimeMillis();
classicIO(args[0],args[1]+".old");
long t2 = System.currentTimeMillis();
newIO(args[0],args[1]+".new");
long t3 = System.currentTimeMillis();
System.out.println("size: "+(new File(args[0]).length())+"
CLASSIC: "+(t2-t1)+"ms NIO: "+(t3-t2)+"ms");
}
static void classicIO(String src,String dst) throws IOException {
BufferedInputStream in = new BufferedInputStream(new
FileInputStream(src),BUFSIZE);
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(dst),BUFSIZE);
byte[] buffer = new byte[BUFSIZE];
int nacteno;
while((nacteno=in.read(buffer,0,BUFSIZE))>0) {
out.write(buffer,0,nacteno);
}
in.close();
out.close();
}
static void newIO(String src,String dst) throws IOException {
FileChannel in = new FileInputStream(src).getChannel();
FileChannel out = new FileOutputStream(dst).getChannel();
out.transferFrom(in,0,in.size());
in.close();
out.close();
}
}
Kdyz spustim:
size: 4589 CLASSIC: 1ms NIO: 12ms
size: 37400824 CLASSIC: 135ms NIO: 121ms
tj. pro dlouhe soubory se to srovnava, ale pro male je klasicke IO
rychlejsi. Jak to ?
Makub
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Supercomputing Center Brno Martin Kuba
Institute of Computer Science email: [EMAIL PROTECTED]
Masaryk University http://www.ics.muni.cz/~makub/
Botanicka 68a, 60200 Brno, CZ mobil: +420-603-533775
--------------------------------------------------------------
smime.p7s
Description: S/MIME Cryptographic Signature
