该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。 f7 H1 [8 M+ a7 A/ [0 Y
3 A7 r" }$ o m6 k/ M5 {) `
package test; y0 X3 ~ Z, J; r4 w- C
' x) y& e8 o, h7 Mimport java.io.BufferedInputStream;
7 [. q- G* q/ U {" n6 w/ i! Dimport java.io.FileInputStream;
" a% T' U; J/ E( {/ mimport java.io.FileNotFoundException;
( ?5 c7 M# c3 Y3 O2 Mimport java.io.IOException; / F, {# a" T u) Q
import java.io.RandomAccessFile;
# f k" u. n# h% _ Fimport java.nio.MappedByteBuffer; ( Z. G2 u( ]4 `" D5 l8 c
import java.nio.channels.FileChannel; 7 k: [) e4 M. V' m$ k& A h* [7 v
D. j3 H2 d$ w7 A6 Q% [
public class Test {
6 ~5 Y# d8 d9 G1 {0 L, r U: u
, |& }6 p% d( t( i$ a9 r$ f$ F+ S: Z) F3 w9 z) h$ y0 k
public static void main(String[] args) {
+ k, D. \3 ?7 a! g7 k try { ) L7 t }% y/ n, `# L$ z& O, p( \
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
7 c, d6 c* K) t4 t( H9 f int sum=0;
3 y) y q/ U, x5 D a4 u+ ] int n; 4 h% I4 }# I( L9 @5 z
long t1=System.currentTimeMillis(); % a+ `4 J: a9 n9 Y0 {& d1 ~+ t, r
try { ( q q3 g( U- D6 T
while((n=fis.read())>=0){ 8 m9 ^0 R8 k+ ~
sum+=n; 9 R9 |! n& Y6 L* [$ g# H/ j3 V
}
: P6 _) c) `/ m: X, B } catch (IOException e) {
/ k) F$ r! T6 D f0 v& Y% j D/ m3 K // TODO Auto-generated catch block
4 [% E i+ y. n* i" d7 N e.printStackTrace();
6 a9 ?3 e2 j! z( c3 M$ V0 P } , ]: _. C+ _1 d8 U6 r; x$ r, J7 j
long t=System.currentTimeMillis()-t1; & O( M+ M) r3 [
System.out.println("sum:"+sum+" time:"+t);
! b- W. ]$ j% j& H- V8 P } catch (FileNotFoundException e) {
, u d# O2 c9 x) i& `' a8 k // TODO Auto-generated catch block
; Z8 d, O. G# U5 \ }! K e.printStackTrace();
9 r% p# E' `2 y" F2 L) ` }
% k7 n% h$ x- W
7 G2 }6 I- u% D6 ?# B try { ^! O1 J$ L8 G, T
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
1 G. S) R1 |7 F BufferedInputStream bis=new BufferedInputStream(fis); + G z- {' }: q- g
int sum=0; + ~$ D. p' O! M/ h. p- N& Z
int n;
7 ^; p5 ~* k H# A! x4 r long t1=System.currentTimeMillis(); & _* P: a0 ^, _& ], Q/ X
try {
. ?5 r( f% x5 L* N while((n=bis.read())>=0){ & _5 u( _8 x1 b- t2 P
sum+=n; 8 [, B/ N* Q! ^ V8 _
}
6 K2 j" g( Z" u: \ } catch (IOException e) {
' p, X e# O+ J* \2 o" `/ q // TODO Auto-generated catch block
: N. g. l! a9 w1 x1 }* Z. n% O e.printStackTrace(); / Q' c, \( q/ x/ x
}
5 V/ U+ x; u" A/ J d! j long t=System.currentTimeMillis()-t1; 7 S) J4 r1 l) n4 r% m1 x
System.out.println("sum:"+sum+" time:"+t);
% h, u! B# P0 z' c5 b5 q. F } catch (FileNotFoundException e) {
. J: z, l" r' P5 \6 W // TODO Auto-generated catch block
& k6 @: J# e: c# f, k e.printStackTrace(); # t% g+ f* h1 F5 ~0 v
} 6 p4 e: A8 o& F" k3 B
) ^9 |1 D* M1 R: \7 P2 ] MappedByteBuffer buffer=null;
& ~: G4 ?% \7 M6 V' W% E2 f try {
! O8 ^1 z& ]1 q p0 v- [ buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); ( x; |1 i; P9 d. O8 n6 n4 | e: Y7 [- [
int sum=0;
U2 M9 }- G1 J/ Z1 z int n; : D8 {8 `# ]( T. _& y& t4 k
long t1=System.currentTimeMillis();
7 k( V: s9 T- f( T' g for(int i=0;i<1253244;i++){ ' G4 I4 ~# M v3 A; o( P3 A3 T
n=0x000000ff&buffer.get(i);
( W' b. [( i1 ^9 F5 o: T6 d# u6 | sum+=n;
( d; w5 h/ n2 C( @0 |' v8 @7 e }
% r; O' q" u; @$ V) `2 H long t=System.currentTimeMillis()-t1; ; ~/ Q% M1 L4 G9 @
System.out.println("sum:"+sum+" time:"+t); + T& [0 m4 N" n
} catch (FileNotFoundException e) {
5 |7 g N5 t. f7 i H4 \7 _& O // TODO Auto-generated catch block
& d! A% Z6 H; N$ w6 A/ r e.printStackTrace();
6 x6 }: ]4 {3 }- e7 L } catch (IOException e) {
2 B1 A f# }( E3 b // TODO Auto-generated catch block - p; F: ]9 k+ U% f$ R
e.printStackTrace();
; b5 s: g, |7 ^) l5 o } 6 e4 }/ P0 g$ ^6 l) Y( ]
! S) D7 X/ O2 F4 f( l% u } 0 D, n$ S7 R- X) j# b3 P
: V0 O/ |( W; q0 K
} / C; A6 H0 w3 H! ?1 u( A
测试文件为一个大小为1253244字节的文件。测试结果:; _ h* j" {/ J$ q3 A2 X3 k$ ^! W8 }
8 w/ l9 b. f. J+ F' e/ w0 m s3 Vsum:220152087 time:1464 ; x) U2 M" o$ b& |8 ?; r
sum:220152087 time:72 : Z8 n4 e! ` y
sum:220152087 time:25 - E; U' V6 U2 p# B N
说明读数据无误。删去其中的数据处理部分。3 M3 w: o" B: x4 v( [' o
_: Y& ^4 O4 N/ x/ q
package test;
) g u! U9 P/ Z# L
& T, B, L; {# ^/ X* }! ]import java.io.BufferedInputStream; & C) O5 n7 H( F% X: g
import java.io.FileInputStream;
9 b6 Z) o- T- Z2 ?import java.io.FileNotFoundException; : z5 T; f! w, g! | F" q5 Q# a+ `
import java.io.IOException; 2 ~; Y: O# i0 M7 R# _6 L
import java.io.RandomAccessFile; X+ z3 o' N- a( Y; d3 b
import java.nio.MappedByteBuffer; 6 h0 x( X# `7 V! g6 g
import java.nio.channels.FileChannel; ( Y' t- l. e& `5 Y; ?- `7 s# t
' B0 [1 l' k9 x; {public class Test {
7 S7 U. e9 C/ w" ]0 s1 N* z, o! {; `: t4 z
, {6 {( }! D; M8 A8 \* g* Z
public static void main(String[] args) { . ?: E- q, `6 S
try {
+ l. X" ~: I/ [1 e* |# O; T! {9 X FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); q0 R" [. x1 _9 y: {6 R) ~/ |
int sum=0;
; }* \ g O- C: L; D int n; / `3 p `: B+ u- E9 w) N) K
long t1=System.currentTimeMillis(); # g+ l6 o3 O& U5 t
try { 0 P' K' y$ r( f
while((n=fis.read())>=0){ : \; Y* Q/ r# k) g8 B$ c
//sum+=n; 9 N! p( Y& b9 b @+ V
}
8 b! ^" B/ w! K: n& v R, V } catch (IOException e) {
4 U8 w. `- F3 ? // TODO Auto-generated catch block
' \1 D8 \7 E# G& M! h e.printStackTrace();
5 J/ b2 w2 w8 g* a# \ } ; b! G( X& a/ C
long t=System.currentTimeMillis()-t1;
1 L6 Z7 b1 {+ G0 m k System.out.println("sum:"+sum+" time:"+t); : ^2 N& m! F8 D7 `/ k
} catch (FileNotFoundException e) { / n2 v. D" f9 H. i1 d
// TODO Auto-generated catch block ! ~: b4 p/ A* J$ v2 e' _ w5 o E
e.printStackTrace();
7 Z. L0 k Q( P }
, N6 S# I, L5 p" Y5 O% D3 Q
# `" C! S u- |/ ]: A try { ( U0 P$ j% ^( s& o$ }- b! z
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); : r! Q. q# ^+ Z4 z/ }: E S- U
BufferedInputStream bis=new BufferedInputStream(fis);
3 w) X4 `* n; M3 v' U1 {; b int sum=0;
# s- K3 O; c7 c4 d7 {* E; y6 a int n;
$ u- N" K6 v8 q, l2 o$ k long t1=System.currentTimeMillis(); - U5 F, x; ]5 u2 J9 T
try {
j, u6 L" c: L while((n=bis.read())>=0){
1 F2 v6 C, _2 E& W //sum+=n;
/ V- t0 r6 |* _" i) q } . q+ I% T0 h2 x) P/ d
} catch (IOException e) {
4 R, j( C) j" Y5 @( n# S3 S- ~ // TODO Auto-generated catch block % Q- l+ u4 ~/ K
e.printStackTrace();
6 Z0 j* G* C. w+ H }
2 ?- h+ e+ j, u' Z: {: j2 x8 y3 r& ] long t=System.currentTimeMillis()-t1;
- T* O E) W8 x System.out.println("sum:"+sum+" time:"+t);
+ f: u& S, |8 m) s( P& d& y( ? } catch (FileNotFoundException e) {
6 t' }4 x: C( M! n | // TODO Auto-generated catch block " F0 J- V( a& R! a3 q% F
e.printStackTrace();
6 D" d5 R R) O# s# e } ( L2 T# ~. B, I* n6 j3 z L% b. F
, A8 t3 O+ d& D6 I6 n MappedByteBuffer buffer=null;
0 ]- [$ m/ ]$ J. N. c2 e try {
, o0 ^& V G, ]" K ? buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 4 M- B. S2 h7 H$ D
int sum=0;
. p9 W4 A5 s$ ]: ] Z1 E! b int n;
9 n3 \% D K! D: `3 P4 e long t1=System.currentTimeMillis(); , g. V0 e$ H4 |; y0 I
for(int i=0;i<1253244;i++){ w- l; w' S0 c; ^% A7 J& N
//n=0x000000ff&buffer.get(i); 3 A; m4 D( y; d& s! H/ X( t+ n
//sum+=n; # H# _( b' m b! B
} $ v/ L2 D5 H7 \" a4 v
long t=System.currentTimeMillis()-t1;
4 ?4 f( [/ H/ i: T% v- f System.out.println("sum:"+sum+" time:"+t); ) E: D- r3 c6 G
} catch (FileNotFoundException e) {
+ { I P5 e- X+ ^& g ?4 ~ // TODO Auto-generated catch block % U' p. }3 Q& o5 K+ \; G% G
e.printStackTrace(); ) a% l2 j1 r; [2 @, E
} catch (IOException e) { ! R" n# `" o+ a0 L
// TODO Auto-generated catch block
5 U7 D7 Y0 c8 J6 e e.printStackTrace();
P6 F' H$ F% m }
, R z4 N4 ^4 B6 e- Z6 l) H
9 q; P) j% |5 A" H1 n/ t } 6 g: x, T* v% t; m* b
" h, N! t8 ~" C; L}
0 Y8 V2 n. u, q. Z m* b# O测试结果:
6 B( p% Z" \; T/ k3 s$ k4 a; O8 c, f* U; w0 w0 T( _
sum:0 time:1458
- x7 F9 {1 p) {; c; A# {* s' K' Bsum:0 time:67 $ {* E) L7 X3 n8 H
sum:0 time:8 % b) g2 R$ W' N3 y& Z& p" D- C
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
$ [$ S4 L: q* ]3 i0 S: p5 l3 L8 O5 C' c: ?
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。& t; V7 G4 y' w# k3 g$ i [* e
% e" J, F1 @) F. a; S, f3 F0 A |
|