该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
& s, e0 i8 Z* s& ? G% J4 l _. F+ y$ ^0 H8 T
package test; ( `! Y$ P* D, ^6 p c) k9 W
* r- ?0 o! R; C* M% }3 J m
import java.io.BufferedInputStream;
2 n# {; j, X& n9 `6 O( Simport java.io.FileInputStream;
; b: G8 L: S5 P8 j7 H) R9 G, e7 oimport java.io.FileNotFoundException;
" Q1 E% ~4 ?% y+ E1 fimport java.io.IOException;
0 s- c( n2 r7 x% q. A' Limport java.io.RandomAccessFile; # g6 w$ {2 J7 Q7 q
import java.nio.MappedByteBuffer; * p- p( I$ _4 U, }" u
import java.nio.channels.FileChannel;
5 g3 ?1 j7 R" e2 p
& C7 Q8 t5 j3 n4 ~8 Q" ]+ spublic class Test {
- \9 R) A4 a3 D, x& z' `4 C# C+ ~ p
' `: ?5 H: P- ]# p
public static void main(String[] args) { ; B6 [; c+ E6 I8 O+ E
try { * R6 @9 w6 c& R6 v& v
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
( n' y9 G; b/ h) W4 P int sum=0;
! Q: @6 h7 p) g int n;
) R4 L6 x) p& {9 u$ \7 [- S) E long t1=System.currentTimeMillis(); * f6 R: I* v& }& j4 T
try { 5 I5 w( P/ N3 r9 ?% K
while((n=fis.read())>=0){ / D8 B& _# E1 ?
sum+=n;
9 V2 b& a8 X6 y( m }
. n' ~; r# H6 ^9 |6 d; R } catch (IOException e) { , i8 @1 k- ?% H s/ A
// TODO Auto-generated catch block
' w9 i/ x# m+ `/ ~6 I* O! c e.printStackTrace(); * F W: i& m2 W4 N# e
} 9 u" g1 Z* h, t: l
long t=System.currentTimeMillis()-t1; 3 [. v; x9 L# \+ o, _8 _0 n0 d
System.out.println("sum:"+sum+" time:"+t); 3 F0 O) C( { G$ Q% @' c
} catch (FileNotFoundException e) { 9 A* W$ f. j& U1 j6 \4 c
// TODO Auto-generated catch block ( T7 Z# v! P3 U. z
e.printStackTrace(); 7 _6 l8 v4 o m1 I. ? d, [
}
# C" J: m$ \: {0 q2 c* m5 b) i2 O# I# Y) s
try {
0 O# ?5 V2 ~6 U f FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); . m' \8 a; P: w |7 H. }* g
BufferedInputStream bis=new BufferedInputStream(fis); $ _4 Q+ k- C+ C) i
int sum=0;
8 t8 T3 ^ Y0 m* Z$ P3 ]7 e int n; . A8 q w2 e0 `: n: j7 g$ O' B: _
long t1=System.currentTimeMillis();
T2 I$ Y& {3 h. p; W6 S4 z try {
2 `3 G" B9 E7 G, I; ?% a# G. Q; C6 Z while((n=bis.read())>=0){ % @8 h, S# @: [) s+ c( C7 r' E
sum+=n; , A) L: m2 l9 P f6 D0 s7 e- Z
} 9 ?0 y, N P3 e+ L2 w" W- N8 f
} catch (IOException e) {
5 W! K1 f3 x7 z1 y6 P: l // TODO Auto-generated catch block
/ f4 g2 J; D" }: B3 c e.printStackTrace(); 9 F2 R, O3 I: M* W
} 6 L( i) i$ P! f; N2 a7 L) w% |
long t=System.currentTimeMillis()-t1; & v8 t$ ?5 p v n
System.out.println("sum:"+sum+" time:"+t);
; y0 P0 Z6 r$ L/ l* d, [ } catch (FileNotFoundException e) { ! y/ _* V$ |; e, H/ a) U
// TODO Auto-generated catch block
+ d8 w! Y" n7 [+ [ m+ b8 S e.printStackTrace();
1 l; i" @2 e+ c/ E4 i }
$ i: J0 \# F t5 E3 K( e5 d" E7 e, t( A' I0 Z6 g
MappedByteBuffer buffer=null;
`1 x! A B j6 g. Z. z6 U try { + G) g& s2 F: t' S6 Z
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
0 n" h$ C5 R f int sum=0;
" g. S( E$ }; f- k% ]. O int n;
e2 c3 L; J' L- |. y G( `7 W long t1=System.currentTimeMillis(); O7 ]4 ~# J ?! p, q
for(int i=0;i<1253244;i++){ 3 \$ @% h! T* O9 W) u6 }
n=0x000000ff&buffer.get(i);
! U, Q4 z* R4 U5 y5 `2 q4 N sum+=n;
: G1 U0 c* ?( V, L- B% c } 0 P+ A# p$ k6 W
long t=System.currentTimeMillis()-t1; . d$ |3 R6 l/ i/ ~2 g+ ^
System.out.println("sum:"+sum+" time:"+t); / |' j/ [% Q: H/ {, p; w1 d
} catch (FileNotFoundException e) {
' o) z: y) M1 f6 Z // TODO Auto-generated catch block
' C0 x6 P7 c! b( R' g: K e.printStackTrace();
& R, e2 X Z" D7 O! P } catch (IOException e) {
* H2 J: F" I& S. H // TODO Auto-generated catch block ( C! y3 X0 y% F
e.printStackTrace(); 4 y$ \1 h6 r' ~+ m
} 3 ]0 B1 G. C5 j% `. r6 z
* M3 u3 S. s0 n9 h+ P
}
4 p O4 E3 G0 V, T8 y- t6 V; Y, d% Z6 W `) }- e% H2 E) e# O/ S, z/ L5 T
} % T! a f' j* w4 L
测试文件为一个大小为1253244字节的文件。测试结果:. C! B* b0 o: I& t O$ ]4 b
3 ^$ Y5 Y8 q( e4 Z& g8 esum:220152087 time:1464
3 h2 _3 y. z/ ksum:220152087 time:72
8 y+ H! q7 s+ _ O0 `! fsum:220152087 time:25 ' O7 w" X* Z P0 q5 _9 O: ` k
说明读数据无误。删去其中的数据处理部分。: W! Z8 _7 `& x$ s; ]
; C; r7 a9 _4 l! X M7 c2 _2 Y% f
package test;
% _! j, A+ q- }( @1 v4 N# r( P' [& V; k. ?
import java.io.BufferedInputStream; + A/ v, O( ?3 X. G
import java.io.FileInputStream; 9 b9 j% B7 L7 O4 D' n3 T
import java.io.FileNotFoundException;
- q7 I( p/ t( v" E0 O! }9 Iimport java.io.IOException; * e: Z: S8 w1 @1 U* W6 B
import java.io.RandomAccessFile; " ]1 ?6 r5 ]5 z# D
import java.nio.MappedByteBuffer;
5 U, w1 _. A4 Himport java.nio.channels.FileChannel; 5 p0 ^7 a) k. q4 {; b( v
$ f2 ~0 x2 X% J k1 R0 d
public class Test { + T7 r* |4 ^0 h5 [1 m E
8 x- a- x( v5 M& P5 ^7 A% M1 w
& C8 O- E+ e# c7 o" P
public static void main(String[] args) {
. d0 h3 q4 G$ @5 Q3 {9 [+ W try { 6 P$ Y% J {& s Q, ?" k8 f7 P
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
& R: J# p$ s4 i' n/ ~( y/ K int sum=0; $ k2 f) N+ m3 q+ M* V
int n; ' g( ?' |$ _2 m6 ~* e/ i
long t1=System.currentTimeMillis();
6 V. P4 P4 u' `3 e4 k+ p E! Y" t try {
" h# e* }: x0 o0 S& V% P( R' r while((n=fis.read())>=0){ ! u b) s" f% P
//sum+=n;
% y% ^0 M' x- x9 ]' z$ c }
* ^. t1 m$ ~* m- {" e } catch (IOException e) { 6 A3 C5 p1 t0 W& x3 S
// TODO Auto-generated catch block
% C$ `# |7 Z9 v2 }$ _ e.printStackTrace(); % ?0 V1 K: c* \: |. {- h) N
} 5 e) E6 e2 v3 ], ^* d4 y
long t=System.currentTimeMillis()-t1; * X& {' V) A6 b) b: e) }2 r1 K
System.out.println("sum:"+sum+" time:"+t); % j9 @, c) u( e Z* b
} catch (FileNotFoundException e) {
* |, ^# @2 H& \" d" H( b7 E // TODO Auto-generated catch block h, a; f: C, L$ ~. t
e.printStackTrace();
# N% k' h3 X. W: i } 4 i: o5 O* M; `" I% I
% T, o, U7 Q: M try {
2 u# j0 p' o3 O2 b3 ^3 i FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
: S, J+ R( A. l* m! F! C BufferedInputStream bis=new BufferedInputStream(fis);
, H1 D' p* f6 a$ q! `7 l4 |3 U. H int sum=0;
, Z: B3 D4 A G/ i$ g2 N$ l int n; - Q0 n- p+ i; w- r& _& T
long t1=System.currentTimeMillis(); ( D: _! g# \. D3 \. j0 t( I( v
try {
/ Q' k* z) i3 B. R D+ G) Y while((n=bis.read())>=0){
, p' P* M \- p$ r8 I //sum+=n; 0 r! Y/ C p* f5 ~
} 5 n3 G# c: g9 U, h+ j
} catch (IOException e) { 2 w F: E# L* |- U* c5 Z; i; n
// TODO Auto-generated catch block
6 \4 d# e' i% l* `% v1 {$ g e.printStackTrace(); 0 k. F) l) X0 ?- l4 A5 T; p+ z+ I
}
: T. a0 W! o& C( N) L( m) M long t=System.currentTimeMillis()-t1;
& ~4 y6 [! }* |! K* w5 B System.out.println("sum:"+sum+" time:"+t);
9 U! p6 M/ ~- {) M. x } catch (FileNotFoundException e) {
& K+ m% B+ m) K // TODO Auto-generated catch block
7 m: w8 ^% D/ _# o: _# v* [" z' n e.printStackTrace(); 1 [' f! ]( _& z! E9 _( j# Q
}
f( V! Q5 V5 v/ f3 q$ j, m: q0 J S/ b
MappedByteBuffer buffer=null; 2 s, R* W# r& @6 Q P& d
try {
* X( n+ A3 m5 S5 I* T. o buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
; t* }/ K; K) N3 `% z int sum=0;
- L( F) o: M: j int n;
" }* \- }3 y, w" a- [: m3 A' t long t1=System.currentTimeMillis();
`1 i* V( H3 _6 s/ w for(int i=0;i<1253244;i++){
# m, Q9 G0 N! S2 W. {" `3 C //n=0x000000ff&buffer.get(i); / e8 z& g" g' w; z3 ?# O9 i
//sum+=n;
Q# a. A7 ]1 `4 E* u }
V! ^! Q. j0 S' \5 ~ long t=System.currentTimeMillis()-t1;
; |2 x3 X6 N" U+ T4 J3 O% R9 @/ U* z. x System.out.println("sum:"+sum+" time:"+t); " A( e0 b V Q( i! O$ b
} catch (FileNotFoundException e) {
( T( k% v) p+ h6 {0 V# [ O2 D // TODO Auto-generated catch block
L1 v+ T* Q* v" O* b e.printStackTrace();
! A a4 [2 w+ R/ k9 R& ]( s3 t } catch (IOException e) {
- q- g( w: a; s; g) m4 V0 X // TODO Auto-generated catch block 9 d, M8 J! c; F4 E' o9 V
e.printStackTrace();
2 F" L9 k# k/ v) m$ P& h, z5 T, Q } " |% Z; }9 {# g* M9 h( l$ F
C& `4 N& C( H! c" s1 S: G } , W# P% X5 \; F" ]1 B
, s. |# M M5 X/ ~# K8 H4 C
} 3 _& C" N9 J& J1 [' y
测试结果:5 f1 z; {* K6 z C/ Q4 T. a
1 L% i7 Q) X1 U
sum:0 time:1458
- J4 |9 I9 K U5 i. g$ @sum:0 time:67
( v$ |% V/ Q. C" r5 Xsum:0 time:8
$ b; x" S, F6 ]" F% B2 p由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。, I+ P- S2 P+ ?
0 k7 Y5 N' J# s5 U
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。
, Y2 @( U+ B- J- T% o: U8 }. u: E1 Z1 ]3 p( @6 O
|
|