该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
: H' r$ }* I+ z0 `2 q4 S8 Q+ i4 j* _7 k8 f q$ R( r; G& w
package test;
2 B: Y; {& z! `
& |* ]& `: e8 v, Q: ?, a# [import java.io.BufferedInputStream; ( H3 Q: S4 `, G$ o; w1 }& Y
import java.io.FileInputStream; + L7 c: y- v( X1 ?% Y
import java.io.FileNotFoundException;
! Z: {7 G: X( o H9 v3 R; bimport java.io.IOException; 5 X; S9 l, P* U+ G. G- j5 A
import java.io.RandomAccessFile;
7 g; B( W x+ r" R$ b6 Cimport java.nio.MappedByteBuffer;
' n* [: q- W2 ~2 A0 z9 `% _import java.nio.channels.FileChannel; 5 L" B$ _5 D! V4 a: D
, I5 H8 T+ T d+ [( t! qpublic class Test { 8 w2 n. Z4 W& [) S' n# F% ]
; p: h' w9 i' Y* [3 F
$ v9 W% _ |* O. C
public static void main(String[] args) { / d2 {: L5 F G2 \
try {
4 F5 a, v3 h% |) M, | n FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); % y$ Y6 r1 q5 Y4 y' c0 Y
int sum=0; - a3 r2 K Q0 A' f' j! `7 o& P9 }
int n; $ O Q& F" |2 ]) O0 u: b
long t1=System.currentTimeMillis(); 9 g: r% r, T! ^6 S* d+ U. b
try {
7 s2 R K3 S3 i$ U2 x, u, b* k: _ while((n=fis.read())>=0){ 2 ^8 X: B# ~% l$ N. R
sum+=n; % M$ ?: Y# @" g* N3 u0 ~
} # N( K8 O3 U; k5 n2 w
} catch (IOException e) { ) t( b/ J) X0 N& {+ Y( s
// TODO Auto-generated catch block 6 _/ V/ P$ ^. @' G8 S
e.printStackTrace(); # ^, O( s2 j R4 a) W+ R! _
} 6 U% Q% d1 B6 D0 _
long t=System.currentTimeMillis()-t1; : E: B; v6 m3 t
System.out.println("sum:"+sum+" time:"+t); * _1 {3 p: W% | }
} catch (FileNotFoundException e) {
+ O. ]% @1 U0 v6 c9 d* Q4 _: g; [ // TODO Auto-generated catch block # b/ y5 e1 U2 G% p
e.printStackTrace();
, ?* m1 Q/ `0 v% B) I- d! z, A } 0 Y8 _4 r% Q$ D: P: n
2 z ~2 X9 q# g try { / O3 u" `# S2 n- k: {, S
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
3 c% ~9 Z7 @: Q P1 p BufferedInputStream bis=new BufferedInputStream(fis);
! ~/ b- ^0 i3 f: y* H3 z int sum=0; , C V6 w% e1 W4 ]+ x1 `8 J: G* G
int n;
9 R0 y$ C" j2 c% C5 R" D- K long t1=System.currentTimeMillis(); ! G& s& \0 C! r- P Y* R+ M8 a
try { : {4 O+ i7 X0 c4 r8 B
while((n=bis.read())>=0){
3 A+ G/ v( Z, @" R6 c sum+=n;
7 r% v" z n2 H3 z }
1 Z# O# } n% y; l$ S } catch (IOException e) {
/ {/ [% i2 C; R: O- { // TODO Auto-generated catch block 6 x- |' K4 v+ R3 x( Y. T9 r' F T
e.printStackTrace();
) u! K, \) T/ T$ @ } ; u9 s) I5 V$ g
long t=System.currentTimeMillis()-t1;
: \9 N8 o3 h- \- X System.out.println("sum:"+sum+" time:"+t); ! C9 Z) v9 `# I6 }; @
} catch (FileNotFoundException e) {
0 t1 F0 H) y4 J) B // TODO Auto-generated catch block
: Q% e5 L$ Y) P( a- ~ e.printStackTrace(); + x/ y5 T) G I0 V
}
1 n0 _/ @" Y8 n$ A& J4 [ x8 E8 E; s# h: X2 A$ \
MappedByteBuffer buffer=null;
; a' d$ A; s! X# j6 f6 M& K try { * s! Z8 E# U0 K6 |
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
8 \7 Y+ y4 G( [) u5 [1 z% X int sum=0; ' r. w! q- f: |# U
int n;
) b/ i2 }7 P( ] long t1=System.currentTimeMillis(); ( h( e4 g& H9 s8 q! a
for(int i=0;i<1253244;i++){ 3 L- u/ n) _4 ^6 _& N9 |
n=0x000000ff&buffer.get(i);
' g4 C* O X) p- t2 o6 G0 ~) l sum+=n;
6 D8 e! E' S" g' k2 a } 1 C2 O8 U0 y D( U
long t=System.currentTimeMillis()-t1; " V- d0 I5 i) ]
System.out.println("sum:"+sum+" time:"+t);
: t( |' S6 V4 A' n5 F } catch (FileNotFoundException e) { ' v# Y( t, I/ {4 q: O( T4 h D
// TODO Auto-generated catch block
' m5 c' z' t2 H6 u e.printStackTrace();
. `4 I6 d/ |3 q& M& F/ A } catch (IOException e) { + F. Q: J" B) d0 V% W
// TODO Auto-generated catch block
" r1 N! ~# ]5 ?! j1 Y* \ e.printStackTrace();
: b+ W0 U4 p3 W2 S% [4 d& K } 8 U% U# ]5 u6 @+ ], T: E
6 h$ q# b' s* @" E- F# c; {. D6 h
} 2 a& J, m# [% ]6 w4 Q* K% u
3 E: F. `/ r( f$ |& c$ r
} & ^! e, E' ^4 Y4 E" W, I# S
测试文件为一个大小为1253244字节的文件。测试结果:3 ~* y4 z9 G+ F/ W3 Q
E& E7 ~8 \* z! |% Q) Qsum:220152087 time:1464
& _) {# e' H3 G$ Ysum:220152087 time:72
' J! r( k7 |, m8 Tsum:220152087 time:25
! N$ \2 r! B' \0 Y" H1 Q说明读数据无误。删去其中的数据处理部分。# Q9 H9 ?; S; F h' o& T! l [
0 G% ~8 y) e% \1 d
package test; : e" t e$ M1 ?) Y( P
0 Q: }' `: o% u! {8 O, aimport java.io.BufferedInputStream; " V# \# O' k. J, O
import java.io.FileInputStream;
3 C" b. J5 S2 \" c" O, himport java.io.FileNotFoundException;
6 b+ q( i# T( kimport java.io.IOException; 3 v0 d# z8 F# ?$ h4 r0 |
import java.io.RandomAccessFile; 9 ?4 S/ i" G5 i0 U& F; I
import java.nio.MappedByteBuffer; ) n/ F# }$ [8 g4 A( Z2 N6 X
import java.nio.channels.FileChannel; % L2 {% [! `0 O- w) W( W
1 Y/ |: j6 ~- K
public class Test {
9 N' S& ]* Z$ e- W& J7 f! |
0 U8 r9 ^" @- m1 y5 J) J* N% x3 |
; ]# N8 W5 p0 z/ W( a public static void main(String[] args) { ; f5 G) `- d4 N
try {
5 V+ } F7 i2 Z6 K2 V FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); + _3 ^! c" g6 ^9 j7 S% B$ T
int sum=0;
- H' g/ `% q: o" ?, d int n; - G7 J2 x4 ?1 u% _! I
long t1=System.currentTimeMillis(); " e. R+ g2 Q: d! c) [/ |: s" A
try {
& v4 c+ k2 Z* e; a0 k* d9 D2 D( X while((n=fis.read())>=0){ " L; a; u; {! B0 {# M% \
//sum+=n; 4 O0 w4 c3 F' C+ D# J% Z# h0 Z& d. u, L
}
4 ?" ]% I: H. ]2 v$ @- v( E } catch (IOException e) {
& E, t1 [2 c* k+ o // TODO Auto-generated catch block * i4 |: y* v7 i
e.printStackTrace(); 6 ~& I, Y8 ~& \* @0 X. a
}
$ _3 M b; A* ]- ]/ A long t=System.currentTimeMillis()-t1;
# Y3 [, N. Q/ p% f* T System.out.println("sum:"+sum+" time:"+t); " d/ F [* \' ]. K
} catch (FileNotFoundException e) { 7 }1 o7 O4 x& }, W1 n$ h4 Q9 x
// TODO Auto-generated catch block
. D" ^- M( ~, I7 d1 d# u& S7 p e.printStackTrace();
- k2 M5 V7 z* _' F }
) V7 I4 h- R- J; u: i m4 H2 [) s! v) w
0 g8 T I8 G5 K+ V$ b: H try { ; G6 [' V3 Q' K w1 S# ^; v
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
! L ]7 }$ l6 p5 p/ C BufferedInputStream bis=new BufferedInputStream(fis); % i6 t" k& J6 F5 v G# g
int sum=0; 1 W( ]9 O# R7 p4 B% b* Y {
int n;
! n) T1 P* l( f1 {# y long t1=System.currentTimeMillis();
! @( M1 y* O* C+ z try {
! o+ g. o0 r/ b* ~ p9 b3 ^ while((n=bis.read())>=0){
' L! I( a3 [5 o1 o: M: A //sum+=n;
" @8 V, w# e% n+ }+ Z } 8 h& ?, o P9 R8 G" t0 b- \; f `
} catch (IOException e) {
, I7 ?% {) p5 u- R9 h) w5 x5 Z // TODO Auto-generated catch block 0 b* J1 t$ A& M& |- B/ U9 q; x
e.printStackTrace();
; w* V% t3 t% z, h }
; F) z; q2 q% I( M! o long t=System.currentTimeMillis()-t1; D' a& h: N$ D# [0 ?8 K
System.out.println("sum:"+sum+" time:"+t);
8 L+ r0 p. E, D# ]6 r4 Z } catch (FileNotFoundException e) {
5 r# i( R% ?; k' Y+ s/ v4 ] // TODO Auto-generated catch block
* U7 H. d1 N1 h e.printStackTrace();
1 @# h( ?; z, \% }: [3 g# [ } $ c& s3 t* K, t2 L9 i
- K, n; F( G* g+ y$ Q, l) C) n7 U
MappedByteBuffer buffer=null; ( m1 H; _6 `* y; O9 J9 I' q8 w. ^
try {
! x+ e9 E. {+ E% \, L" `6 H buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); + `% {5 N9 I L; k2 H3 ^ L( d
int sum=0;
! p# U* V7 {' [ int n;
% \/ {( Y8 B# w long t1=System.currentTimeMillis(); ' I5 c# s% R6 J, ~9 K) t
for(int i=0;i<1253244;i++){
) O; ~- X6 _3 }& R/ ^ g2 E& o //n=0x000000ff&buffer.get(i); ( @5 E9 F0 k" I: ?1 H, I# I
//sum+=n;
, u3 w1 M' K7 `- |8 L6 q% \ }
% c* v, n3 s2 c7 F1 I+ S7 g( K5 K long t=System.currentTimeMillis()-t1;
& Q/ n, d% ]1 \ j System.out.println("sum:"+sum+" time:"+t); # {; O7 o d% p: e) J/ r5 D I2 e
} catch (FileNotFoundException e) {
$ h. g# M6 U" P( v& p, I // TODO Auto-generated catch block n, q2 [& ^8 i4 s' V* p _
e.printStackTrace();
5 W9 L% p) q1 X8 ] } catch (IOException e) {
6 ^; |0 q) c( [+ w // TODO Auto-generated catch block 1 G" J' \4 M/ Q
e.printStackTrace(); 6 I9 p- X+ j7 r7 d6 A! _ ^
}
; g9 ^8 x( f! B8 B2 ^2 T: f8 c' E* I! F' F! V0 H
} 9 Z# ` W3 v/ f3 [' S# \* s; n
8 f4 S, Q' I& Z+ X9 B6 O) t0 t% ~: {% p
} 7 c" N/ A* @! A! M9 O
测试结果:
: Q; J% h( L [, k6 y9 {0 }2 r
3 @$ l0 }* N b$ Tsum:0 time:1458 4 P3 J3 Z( V$ Z( f2 T% d( Z
sum:0 time:67
9 H+ f6 B0 {* D+ Q1 wsum:0 time:8
; |+ H" @5 w1 t5 u0 _$ [由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。7 P/ P [: _/ {
4 F" {6 Q2 k6 ]
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。
; U! w }# h$ R3 X# ]$ g. a" _1 l, W
# I, C% Z7 A) Q# S: J1 Q: h |
|