该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
! F! O; u, K b8 [: k) Q9 `3 Z Y' P+ D! t- y+ i& a
package test; % d+ M% G; }# d$ M
6 O3 O3 H3 o, z! w3 qimport java.io.BufferedInputStream;
0 p8 n9 K1 ]4 Y5 y/ ^import java.io.FileInputStream;
) Z- F2 n. j& {, c6 Oimport java.io.FileNotFoundException;
' K5 _+ i/ G: d7 z$ q. Timport java.io.IOException; ! F1 t( R3 U1 D5 Q! {1 [
import java.io.RandomAccessFile; ( }6 i& L, k) U
import java.nio.MappedByteBuffer; % h7 H" e8 ]0 p5 j/ ~6 x7 I0 `* N
import java.nio.channels.FileChannel; + l+ `6 P" f& q5 t& e
: K' k/ l6 W8 Q& i6 a7 \
public class Test {
; L/ {5 Q0 W; ^6 Q1 E2 z8 K3 \. Q0 v' K" A
, u6 O9 H9 \* e4 V, D: Y
public static void main(String[] args) {
0 w4 `0 c+ ^& b r& k3 d+ j/ P7 P- ] try { ' K' U/ R+ M( n. Z( h1 C
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
2 w: ~9 Z; Y2 y; Q int sum=0; 5 c/ n& e& o3 y4 Z
int n; 5 ?: Z! e; _5 S' Y/ r5 n5 ~2 x" ?0 Q
long t1=System.currentTimeMillis();
1 i; T- c+ F, Z4 ?' d try { * m; j5 \) m7 j$ R& a% S* P8 l0 K
while((n=fis.read())>=0){ - j# |" R8 } O9 O9 {! R: \+ T+ k
sum+=n;
5 O7 K! {! `2 v" | } * ]# l9 D1 D J2 s
} catch (IOException e) { : x F' h; A# G
// TODO Auto-generated catch block : A' l8 n4 j. O- @) G! L
e.printStackTrace(); + H( g* ^4 B7 L+ s
} 0 }- M& F. W+ ]( m( j; x( t p
long t=System.currentTimeMillis()-t1;
- W& k' c# o7 H$ g" g5 N h System.out.println("sum:"+sum+" time:"+t); ; a5 m5 Y& z) E q1 n. q+ F* G2 j
} catch (FileNotFoundException e) { * i( {6 c s3 M7 H a
// TODO Auto-generated catch block 9 F1 A# V |2 E1 C- ]. A5 I
e.printStackTrace(); 5 T; y+ q& l/ Y, ]) f2 |
}
) L# p& x6 P' ]3 n: U* r' \5 [) `( V) `- i# p) a4 l$ P: X
try {
}5 x- N4 \! X0 S; ] FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
" [( U! Y; p: Q- ~, ^! b- q8 ] BufferedInputStream bis=new BufferedInputStream(fis);
+ T# A- w* c5 m' k7 H0 I int sum=0; 0 o) p& f' z: ]9 u% D% ?2 S
int n;
& t! T4 U0 J4 |7 N2 F: Z( `' r long t1=System.currentTimeMillis(); * p5 |$ Q* x4 Z# ?. S, j% c
try {
9 H$ n" @& a/ A d% n9 x while((n=bis.read())>=0){ * O# p, }1 c! V: b6 s6 y
sum+=n;
$ a# w! x0 }$ p6 H) N" ? } , W% c) W/ X* I
} catch (IOException e) {
; o1 P1 X+ l( k' Q/ F5 b. T8 C // TODO Auto-generated catch block 5 b: M5 x; e8 {4 w
e.printStackTrace(); / F% b4 T/ G+ d0 l- H( R
}
0 U$ L S0 Z& M: o0 V+ B long t=System.currentTimeMillis()-t1;
7 m$ _3 d8 x* ?, M" [; z System.out.println("sum:"+sum+" time:"+t); . h. ]/ [% B& W: d* q, L* q% y- t
} catch (FileNotFoundException e) {
5 G5 u: }+ u1 r$ C8 X* m // TODO Auto-generated catch block
' i9 y6 m9 p. G e.printStackTrace(); & F w4 ^1 T4 w) g! l
}
% q/ W8 Y5 p) L: }, I1 p, o. u5 ~7 R3 L3 x# ]9 N
MappedByteBuffer buffer=null;
1 Z( h* k% G/ d+ W try { 2 f; f1 C" u/ p Q/ J; Z6 B/ q
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
# g1 V, O$ O' _ int sum=0; ' i- s+ i4 a2 L" H2 l6 N7 G o* }
int n;
* G5 r3 d! X. ^& G( S long t1=System.currentTimeMillis(); 5 ^. Z, z3 p3 P3 Z! ~
for(int i=0;i<1253244;i++){
! I3 K4 U! V& n' n: E* O$ H n=0x000000ff&buffer.get(i);
. Q; h9 M# p" d ?: P$ w sum+=n; " |4 B$ W+ x( o/ [9 s) m1 H& A
}
: V' T3 I) ]9 v2 } long t=System.currentTimeMillis()-t1; 4 h) U. S# Z: @+ F( P- r* t( U
System.out.println("sum:"+sum+" time:"+t); ! O4 ?8 k2 K& }2 {
} catch (FileNotFoundException e) { : _% g5 E: S" y- |- n7 B! |
// TODO Auto-generated catch block ' ^' S- f6 X( Z: g( |( T/ X: t8 v
e.printStackTrace(); 7 E( r/ ~% G2 d7 ~# t$ w- [- m
} catch (IOException e) {
# q4 c/ V. u, V/ C7 J // TODO Auto-generated catch block : k! d* a8 M. i% D+ S; H% X" q
e.printStackTrace(); * y% E7 U: b; p4 I( {
}
2 b% |& g! [+ G( O/ x. G) l, q2 ?4 u; X) j
}
) M$ E! Z$ V b8 C( B, Q
- N; ]" M5 q# n1 L/ y( h a}
! E. N% N A) a, p) H2 E测试文件为一个大小为1253244字节的文件。测试结果:
0 _* ~ |. }% |. S4 u( z8 E- B
0 D0 k* L- | \ ?9 J: e! ?4 Z! h8 {sum:220152087 time:1464 9 `" @( X W$ V$ i& ~0 R% k( {$ o. m; y
sum:220152087 time:72
8 X8 n3 _" h* Q+ Y' s: E6 K9 f) \. V7 tsum:220152087 time:25
+ |; G' H6 ~- y( `6 S9 ?2 \说明读数据无误。删去其中的数据处理部分。/ e% O6 k! z) H' p4 B+ t" c3 k: P' F
~; c) V6 P* f* Spackage test;
, [8 u* G: ?7 ]1 g- W0 C. y: _, q" L4 N' I3 v6 h; Z
import java.io.BufferedInputStream; : x. R* Q0 U/ t3 n* Z a
import java.io.FileInputStream;
$ }- f0 Y+ D* Y5 `, j' Zimport java.io.FileNotFoundException; ; @7 x0 K9 J, r/ ?8 [9 W/ ]: Q- }
import java.io.IOException; ; N- V" T+ p" l& r5 W n9 ^: W
import java.io.RandomAccessFile; 2 p7 p- V* W1 B" o) V
import java.nio.MappedByteBuffer; , j- V# e- ?0 J# a8 j3 N
import java.nio.channels.FileChannel; ' S+ `0 x! [& I+ ?
. ]+ h& F8 j; `# I0 T3 rpublic class Test { & h3 @. K; L% h; h" L' Y
: F* u9 o% E& d U
3 ^* W& P/ j$ h( ?4 G7 J
public static void main(String[] args) {
1 o- J& f$ j: Z* n& F5 ]5 v$ Q try { + Y( C9 W8 k: @9 L9 u+ D: G. i7 b( P
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 9 c. ?/ _: p& W0 H7 c: e% x
int sum=0; 8 u: a# O2 g% u$ c3 c2 t4 l
int n;
, e5 R. X# |2 m9 o$ c long t1=System.currentTimeMillis();
' x3 I2 ]9 E0 o7 j* k3 Z c! X' R try { 6 j9 j! p r4 I1 S( L5 A
while((n=fis.read())>=0){ + T% X7 H* g% `+ y6 ?5 n) I+ g' W
//sum+=n; ; n! y, j3 j, T U
} ) L. n( E" b7 R* f/ O2 g9 K$ x
} catch (IOException e) {
/ F h% f8 B' r3 ~ // TODO Auto-generated catch block & l# v# Q9 x! X8 C& y: A' U6 }
e.printStackTrace(); ' J* g/ v/ S% W7 S
} ! R1 B' o& \' a+ F
long t=System.currentTimeMillis()-t1;
7 M6 x/ e2 R3 I+ \0 S System.out.println("sum:"+sum+" time:"+t);
; F: h* d' B2 P4 L } catch (FileNotFoundException e) {
3 ]' M8 `3 C8 q+ v4 x9 Z9 W- j5 U // TODO Auto-generated catch block ' R5 _( t9 D7 d# c/ P- z
e.printStackTrace(); 0 z% _% c4 D3 q# I7 o; a8 u
} . a% d( F# Z4 g. a; z
! V) w. z# D: T! A4 F try {
; y4 \) c3 _6 @$ k FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
* v8 a# w4 r% U/ C# K* F: N& K BufferedInputStream bis=new BufferedInputStream(fis); ' E& w1 D. K W- b! A
int sum=0; 9 W' M" T5 P8 r# p- n
int n; 3 `" w1 S; j3 w9 x
long t1=System.currentTimeMillis();
% a: _9 S, j B# K- B7 T try {
! W$ L, d5 O% \& `* _ while((n=bis.read())>=0){
- k1 R4 {1 M( W Q/ O8 M) Q //sum+=n;
; r8 P* G, W) d } 1 A6 C/ X/ K0 ~( b1 j! ?$ r
} catch (IOException e) {
! X; w) N6 V# X3 h" B& w" B // TODO Auto-generated catch block
, x: [3 T- S0 U e.printStackTrace(); + i! U; M" \' a. x, _" T3 U# d
} ! r; Z/ K7 e& f) j
long t=System.currentTimeMillis()-t1;
' w( I5 v6 r5 a; X9 r6 o5 f System.out.println("sum:"+sum+" time:"+t);
4 S" |2 u& @1 i) U; t4 r; ? } catch (FileNotFoundException e) {
* J; Y. ]0 \* U( x // TODO Auto-generated catch block . L3 z2 F$ [& n# _3 F8 G
e.printStackTrace(); # U3 v/ c% o, L/ s, ?
} 4 J" d7 `3 M/ }4 w
9 o- ^" |+ A( b4 x MappedByteBuffer buffer=null; 9 @+ s. D& e \8 K6 z. i
try {
7 O1 q% e. W6 k( X: g buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
$ p2 h, o( z C+ @ int sum=0;
! F5 A4 I1 H- T8 n. c' P int n;
* E- [7 P' {, f5 _ long t1=System.currentTimeMillis(); 8 k+ O* i4 n, Y$ C4 {0 ^6 w, j
for(int i=0;i<1253244;i++){
4 s1 X8 X# ]1 V$ z6 t X //n=0x000000ff&buffer.get(i);
6 L3 l" U& p1 P //sum+=n; - u$ [4 @6 P6 N; o; R, H M
} 5 y6 c' E! H; V) X; G& v0 m
long t=System.currentTimeMillis()-t1;
8 I) f1 k2 Y0 W6 C# j System.out.println("sum:"+sum+" time:"+t);
/ u5 @" H0 C+ _3 v" c, @+ F } catch (FileNotFoundException e) {
, b7 w2 y% \$ G% g+ D6 G // TODO Auto-generated catch block . R6 L/ e& s6 [$ q1 g
e.printStackTrace(); " W" F& F6 P$ L% E- E0 Y& d( A
} catch (IOException e) { . @( Y3 W4 e& a5 {
// TODO Auto-generated catch block
& B9 U3 R0 i0 k/ }7 J e.printStackTrace();
0 A4 |+ A' R5 ^/ D: K. P } 6 f7 J- ]7 f( }; j$ @5 ?- h7 \6 f
+ X9 F, L) [8 [' |) g1 s
}
% b2 c: k" t1 Y4 P' v' v6 \/ @) A8 t5 n
} - H i8 g$ s& ^5 P8 ]$ d/ T
测试结果:: c3 J# U& u4 p
5 V3 }+ P* Y& K4 G+ y+ \ }& Msum:0 time:1458 + b( \3 `0 v- v
sum:0 time:67
8 `, V' n. ?4 T$ Zsum:0 time:8
6 y7 x+ C/ d0 |7 X+ f* o由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
4 `3 L# F$ U7 p& {8 B
9 X$ W2 R# a: T( k% l这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。
+ s% @9 w/ r; x: G- ]( s6 I3 C& f. G* j+ ?% z/ y
|
|