import
java.io.File;
import
java.io.RandomAccessFile;
import
java.nio.ByteBuffer;
import
java.nio.channels.FileChannel;
public
class
Test1 {
public
static
void
main(String[] args)
throws
Exception {
test1(
"D:/test1.tmp"
,
1000000
);
test2(
"D:/test2.tmp"
,
1000000
,
1024
);
test2(
"D:/test3.tmp"
,
1000000
,
1024
*
1024
);
}
public
static
void
test2(String file,
int
loop,
int
mapsize)
throws
Exception {
//先将上次文件删除
new
File(file).delete();
RandomAccessFile raf1 =
new
RandomAccessFile(file,
"rw"
);
FileChannel fc = raf1.getChannel();
ByteBuffer raf = ByteBuffer.allocate(mapsize);
System.out.println(file+
" order "
+raf.order());
raf.clear();
//在windows7 32bit 下, allocateDirect反而比allocate慢
//ByteBuffer raf = ByteBuffer.allocateDirect(mapsize);
byte
[] b1 =
new
byte
[]{
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
,
'g'
,
'h'
};
byte
[] utfstr =
"this is a test"
.getBytes(
"UTF-8"
);
long
s = System.nanoTime();
for
(
int
i=
0
;i<loop;i++){
raf.put(b1);
raf.putInt(i);
raf.putInt(i+
1
);
raf.put(utfstr);
raf.put((
byte
)
'/n'
);
if
(raf.remaining()<
30
){
raf.flip();
//因为下行的fc.write将会读取raf的内容, 所以先flip一下
fc.write(raf);
//raf.clear();
raf.compact();
//因为fc.write(x)可能没有完全将raf的内容写入, 所以调用compact比clear安全,经过测试compact不比clear慢
}
}
raf.flip();
fc.write(raf);
long
d = System.nanoTime() - s;
fc.close();
raf1.close();
System.out.println(file+
" spent time="
+(d/
1000000
));
}
/**
* @param args
*/
public
static
void
test1(String file,
int
loop)
throws
Exception {
new
File(file).delete();
RandomAccessFile raf =
new
RandomAccessFile(file,
"rw"
);
byte
[] b1 =
new
byte
[]{
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
,
'g'
,
'h'
};
byte
[] utfstr =
"this is a test"
.getBytes(
"UTF-8"
);
long
s = System.nanoTime();
//for(int i=0;i<1000000;i++){
for
(
int
i=
0
;i<loop;i++){
raf.write(b1);
raf.writeInt(i);
raf.writeInt(i+
1
);
raf.write(utfstr);
raf.write(
'/n'
);
}
long
d = System.nanoTime() - s;
raf.close();
System.out.println(file+
" spent time="
+(d/
1000000
));
}
}
|
在我机器上的3次运行结果:
第一次:
D:/test1.tmp spent time=95519
D:/test2.tmp spent time=936
D:/test3.tmp spent time=420
第二次:
D:/test1.tmp spent time=96731
D:/test2.tmp spent time=831
D:/test3.tmp spent time=402
第三次:
D:/test1.tmp spent time=97534
D:/test2.tmp spent time=870
D:/test3.tmp spent time=425
http://www.smithfox.com/?e=148