该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
+ m6 v5 b& v6 t* J+ t
+ `4 X: c5 G' Z0 P' z+ qpackage test; 1 @$ k* }6 T2 M1 u( v
& g' _8 A g. }2 a( w
import java.io.BufferedInputStream;
* k) t6 h) I T4 Q I4 eimport java.io.FileInputStream;
! B3 a/ h( R; G3 Eimport java.io.FileNotFoundException; ( B6 A3 D* H. x8 K6 F
import java.io.IOException;
5 R) r7 \) m% H6 v0 M3 v0 V% cimport java.io.RandomAccessFile; J0 E4 ^. n: B6 Y. L+ _. {
import java.nio.MappedByteBuffer;
8 A$ g% _( }1 c: ^6 Bimport java.nio.channels.FileChannel;
( V( O0 v; I1 |; V1 R1 d
V$ C$ {2 ]3 N. Z7 ^6 H& I% F9 D: ypublic class Test {
' Q$ a0 |% _6 R. m( @4 Y
( S0 w1 `* d7 s! R& U
) q5 h5 P" t( `! e* p0 T public static void main(String[] args) {
6 F/ }& P2 A- w; X- q try {
: b3 P8 R; R" q! u! \! s/ u" m& [ FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
0 T7 [/ h( Y1 E8 B ?) C5 b% @6 F int sum=0;
: c7 \8 Q5 o% h int n; : e. K+ T! a) h4 T* e
long t1=System.currentTimeMillis();
9 \7 B/ \4 X# h+ M! H( ^ try { 0 n$ E: [9 G1 D& o& K7 m. W2 O9 L- Z
while((n=fis.read())>=0){
& q# }5 w1 w" g6 z$ a. R' `/ N sum+=n;
1 @* }5 p6 W& a0 S }
8 R& X5 R9 b5 c2 X% s: y } catch (IOException e) { 0 c/ A3 l/ D: l& Y6 {5 i
// TODO Auto-generated catch block 1 ^% R# O. ~9 c5 F
e.printStackTrace(); 3 E4 {- L$ `4 p" m2 a. O9 L3 ~
} . ^9 q3 `! ?: t! \ C
long t=System.currentTimeMillis()-t1;
8 }) e- Y& |& b. }2 O7 k; s System.out.println("sum:"+sum+" time:"+t);
* y! [! h6 T0 n* U } catch (FileNotFoundException e) { ) M7 u7 c: z# f
// TODO Auto-generated catch block
0 h! X! @, R4 @" k e.printStackTrace();
7 }/ G' b4 [! ] }
' m/ b' [+ n2 ^7 R7 C0 H; {: }& h1 q& e# k9 r
try { 8 a6 h; y( p8 B7 I* @& q4 _1 U
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
" P3 ?8 ? z( U/ M0 u BufferedInputStream bis=new BufferedInputStream(fis);
* V2 y7 M" f1 i& r9 s! t7 M int sum=0; 5 P7 B5 ~: y; t7 I( Z7 b
int n;
: r: }/ A+ o. L3 O, e long t1=System.currentTimeMillis(); 8 Q( m" d7 r( D0 ^6 R" O2 ^+ s7 E
try {
) r* X" j+ I( I; y2 b while((n=bis.read())>=0){
) K* U P" N* y: ^: s sum+=n; 6 V+ r, X8 f% z& T
}
3 ~/ _; V; X7 L6 y. D } catch (IOException e) {
: [, ~3 h5 [0 }# m* T. D! z6 X4 O // TODO Auto-generated catch block
- \, n5 y; Q; ], ` e.printStackTrace();
" u; ], i/ w) ]. E5 o8 D }
; [9 m F1 B5 @ long t=System.currentTimeMillis()-t1; ( v0 p% ?! _. y) n
System.out.println("sum:"+sum+" time:"+t);
$ I8 T$ x0 ^6 b$ l! X } catch (FileNotFoundException e) {
) b |8 f2 _/ ~& Q9 j9 d // TODO Auto-generated catch block 4 K+ y* n. O, Q- L; @+ }% N
e.printStackTrace();
4 H: a$ T6 H! `; v! m6 c } @: C2 E8 a. F% U/ D& z
; e7 D+ H( I/ b h; S
MappedByteBuffer buffer=null; ' K. m3 F( B, E& }6 P* { r
try {
3 a I" X: k& m3 T buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); ! B# [2 y+ I; M r+ k0 _9 a" I X. C
int sum=0; # m; q4 ]5 h. V9 B( P
int n; 1 J- k _: D l7 E3 q. m3 t& i, o9 L* ?
long t1=System.currentTimeMillis(); ) w/ y- k9 T- y+ }1 L
for(int i=0;i<1253244;i++){ " q& w3 n. A! [5 {2 |6 f6 t
n=0x000000ff&buffer.get(i);
7 W+ r: q# E9 C" c$ d, S/ w sum+=n;
! J+ H; P9 `6 U( {8 J H0 _6 n! S% M } - g) ?; y$ v0 ~& L; q- K7 G
long t=System.currentTimeMillis()-t1; ' d! L' D# {; o% T3 e4 q
System.out.println("sum:"+sum+" time:"+t); 5 ?7 G/ u! y. h3 N+ j( o; _
} catch (FileNotFoundException e) { " b9 d9 r" k, r
// TODO Auto-generated catch block
+ |$ Y2 Z. f3 A7 I; w+ R9 Q e.printStackTrace();
2 C- z! K6 ~) e0 u G W } catch (IOException e) { 9 P7 | A( l& y0 h$ Y) v
// TODO Auto-generated catch block
, W6 }: B* m* e e.printStackTrace(); . P6 M1 Q2 V/ v5 c6 \* Y
} ; f1 X# M& ?2 w
) D8 \1 H' r& J; o1 c; ~ } . n2 \9 k8 R5 |. m' y
7 i T0 S) \; v}
; ^4 l$ R2 X8 _; ^测试文件为一个大小为1253244字节的文件。测试结果:
" I2 |+ L' B5 n |! s8 z: {" O
# A9 V2 |" C usum:220152087 time:1464
6 G* ?7 F; e' a2 [, msum:220152087 time:72
; k7 z- @; l" U2 A( G9 _& Ssum:220152087 time:25 Q+ _, E; h0 W- h, b' ]
说明读数据无误。删去其中的数据处理部分。8 p$ E1 Y5 M6 m/ @9 ^$ \
( L" Q9 y u/ l6 e" }3 [6 y
package test;
' E: S n' B4 R- k0 c- C2 x& f) B% s3 Y! [( K1 M
import java.io.BufferedInputStream;
, M' Q) Q e4 o5 kimport java.io.FileInputStream; 9 S U6 T0 T+ `" a2 i3 u, ^; D7 v
import java.io.FileNotFoundException; 7 C R- R/ j4 }, _
import java.io.IOException;
& O& y# }7 t1 |" P6 T* ^- uimport java.io.RandomAccessFile; 3 N& f0 o) [$ t4 w, t# L1 ~
import java.nio.MappedByteBuffer; & ?* B [* _- ?- A( j
import java.nio.channels.FileChannel;
; U/ I5 Y- k# X! g
. \2 g9 n7 K+ bpublic class Test { ' [+ k( x0 E/ r; {6 c
~* {* J. J$ Z/ |" d
" m0 d+ x" D. P4 e# g5 l public static void main(String[] args) {
# b! I6 M# o) Y) a) t ~ try { 5 K3 }* L# w9 n& s0 P* q
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); ! C. ?$ A: w" T& {
int sum=0; 1 U6 Z& i1 H! \
int n; 2 H, t7 t0 f. S0 Q5 t9 G! Q& }
long t1=System.currentTimeMillis(); / u6 ^4 s; J1 ]; v' O0 v
try { : @ o5 W7 `% n9 p% J6 P9 ^
while((n=fis.read())>=0){ & `/ i6 D( B$ x4 ~; k
//sum+=n; 7 W! c1 ]8 k, m8 X
}
' S1 E9 ~/ E; h2 W3 V- e/ [% h } catch (IOException e) { 4 H7 [7 F1 a) q9 a
// TODO Auto-generated catch block % {, @6 S. [# m5 w" a4 F1 a
e.printStackTrace();
2 Y0 j: r. }$ ^ }
, b, b$ J# o$ I1 E long t=System.currentTimeMillis()-t1;
& p" H# X- L2 {0 N# ~2 W O' Q. U System.out.println("sum:"+sum+" time:"+t); 5 f- g( [; w, S
} catch (FileNotFoundException e) { 8 z$ S) H- E2 o: |& L9 _' x% z
// TODO Auto-generated catch block 5 U! X0 E) h3 q( t5 W ^- o
e.printStackTrace(); 5 S8 [2 {! x& o/ ~; _' i
}
0 J4 ]5 Q* |2 ]* F6 E& G
$ k# X5 |5 \3 W! m) v$ V( N try { ' b. j5 ?" S, y) c% M3 h9 Q$ D
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 1 L6 f" b* l: S
BufferedInputStream bis=new BufferedInputStream(fis);
* r9 a3 [6 E3 x7 d' K5 M int sum=0; 3 i6 a- y' K/ |: X ^; C( h
int n;
1 z7 R; v( h3 C. o X: J long t1=System.currentTimeMillis(); 8 s7 I) Z+ z3 N* L+ D
try {
6 a7 [; a4 y0 ^/ H0 n% I: | while((n=bis.read())>=0){ % U4 C* v9 |7 @- Y2 Q1 k7 _
//sum+=n; . u1 Q! [# O/ s; \
}
5 ^0 ?+ @( s4 y0 c; m" j } catch (IOException e) {
) u& v ]' O: o- k // TODO Auto-generated catch block
7 z: S% ~! l' k) l; X- b7 i3 Y. v e.printStackTrace();
+ L5 W' q" V1 U3 @- f) S+ C+ c }
; O/ F$ ]. n: W% }2 O3 _9 P$ p$ @ long t=System.currentTimeMillis()-t1; $ N4 p9 z1 |; n* V
System.out.println("sum:"+sum+" time:"+t); ( m& h4 s& Z* d
} catch (FileNotFoundException e) {
! Y' p; z- Z) u% l% _6 d // TODO Auto-generated catch block ) A% r( Q3 a2 d
e.printStackTrace();
3 ^! }' w" V8 z5 W } + f: _9 {& v3 L0 V& w% N
& J/ G* b3 m' M MappedByteBuffer buffer=null; ( N1 N7 t2 t! C' X, `/ d- r
try { + p$ B( H1 \" G \" @* `" D4 E
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
% G. K- M l3 Z, \7 E; x; Q int sum=0;
8 y8 ?8 Q# [, C& [/ A int n; 3 k$ g8 ]/ g& x& Q6 }% w
long t1=System.currentTimeMillis(); 9 ^! d4 I9 a& `
for(int i=0;i<1253244;i++){
& a9 {0 y! L' |: @ k2 I5 u4 _: u3 K //n=0x000000ff&buffer.get(i);
2 ` n9 w/ I. x0 f //sum+=n; " h& h) s: w' O/ d$ Q
} % {* @: X" x5 y1 H1 g \
long t=System.currentTimeMillis()-t1; $ ]9 J% |/ n- R* m2 t
System.out.println("sum:"+sum+" time:"+t);
' A. J$ [- t( U0 E3 X9 i: w) P9 K } catch (FileNotFoundException e) {
+ B: c+ y8 ~; W& M* ?3 W: w+ H // TODO Auto-generated catch block
9 s# p4 J( J3 A$ c! g" u e.printStackTrace();
; k3 @ t1 i& Y# O+ C- Q1 A } catch (IOException e) { 0 Y/ X( v) [7 {+ r& ?, s
// TODO Auto-generated catch block # c6 ~$ D9 e x
e.printStackTrace(); , f: q, J5 @% D, _
} 5 J5 T! L+ ]( K' R' @8 o/ y
( M$ W0 m) V# H$ F2 B4 B } 8 @% P3 i% N: I N& o
4 y# V& e4 Y6 t* z$ n- b0 A}
6 O9 a2 J( e c" v- Z$ h/ `测试结果:
5 p, y/ t7 F( c% P! ^6 g @8 m) n/ Y+ t5 F4 N: w& d
sum:0 time:1458
- Z- i0 W. Z: Gsum:0 time:67 * `" }' _/ ?7 t! C' o
sum:0 time:8
6 W% v a: v; k7 |由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
. w& W0 I! ]" M7 @9 K. f! g# k9 ?6 |, a
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。5 O @1 v2 }, C( p$ Q
+ h) P( g, w+ v& b |
|