该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
8 f8 u0 U5 d6 L/ {% [1 q# l1 \$ Y" p4 }8 ?, s5 S' m6 w$ W
package test;
- k/ K6 Z% s4 m9 C* L. T5 D( Y8 B( `3 n
import java.io.BufferedInputStream; `1 E; o) G" w% l' ]. Q5 q0 b
import java.io.FileInputStream; 4 h: z0 ], w+ m z+ F2 U: p
import java.io.FileNotFoundException; + E) H2 ?( T9 C9 G: Q" d
import java.io.IOException; ' C1 Z; _. y' Z1 o- C- S
import java.io.RandomAccessFile;
4 h# C3 s: ~7 N% G, e. a1 Vimport java.nio.MappedByteBuffer; 0 u8 g7 |0 c8 c
import java.nio.channels.FileChannel; F8 w5 Y. R& ]7 S
( N9 L8 P, X$ s1 P2 Opublic class Test { ; c% Z2 [% A* M& S5 Z
% G, ?3 N$ l: k2 r4 Q* F
/ p+ p) m8 e; t o& B l8 c public static void main(String[] args) { 9 @. m9 Z2 N7 B9 K$ ~- K
try { q8 ?6 I6 ?. g n7 L
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 4 j. o7 e* m& P4 i0 ^* f
int sum=0; ! O! T+ l+ C% O; x
int n; 9 w$ p V! W# s& w
long t1=System.currentTimeMillis(); 0 R1 {! i! k, s! S
try {
# n! A; i3 T! b6 b- e* m, h while((n=fis.read())>=0){ % \0 ?& P7 e/ A2 [1 f- z/ z
sum+=n;
/ T! G% K; t! H5 R: ^ }
- h# i! \0 | F } catch (IOException e) {
( A1 P7 k1 S! W) ^# h // TODO Auto-generated catch block 3 L" M- M) ?7 |+ f2 p |
e.printStackTrace(); ! |7 F. d5 t' R% x, U& C% E" I
} % G- D% t; w" k" O1 J
long t=System.currentTimeMillis()-t1;
9 X' u" t" D. H+ ^0 q System.out.println("sum:"+sum+" time:"+t); 5 q4 \3 }! x2 o0 S% i& M
} catch (FileNotFoundException e) { ) F0 [$ U( P' h- ?) M& e, x
// TODO Auto-generated catch block / c0 q6 L$ d+ P7 P
e.printStackTrace();
6 P3 Q' j) w- R% n/ c0 W }
: M8 ^7 f* V$ A% G& b! h9 D( b0 h+ M6 I
try {
4 ^ R4 _' I3 t6 U" n FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); $ l0 Z' J, I, U# i; J4 S
BufferedInputStream bis=new BufferedInputStream(fis);
% i3 B' o" T; B5 B* e& S& h int sum=0;
0 i# l/ i! t% u' { int n;
! C# c% w- g9 s& h long t1=System.currentTimeMillis();
9 {/ u0 Y. D" V try { # U2 |4 Q) j6 k2 z9 P; G8 Y
while((n=bis.read())>=0){ T+ O x6 g/ x+ ]0 N4 y5 C
sum+=n;
8 f$ F5 j: G" f1 |9 N7 a }
$ |) Z0 w3 p* w } catch (IOException e) {
& j+ t& O/ P* }; r2 m+ H6 _ // TODO Auto-generated catch block 3 W; r+ M U3 e
e.printStackTrace();
* [6 `- X M3 I( R2 W2 J: k } ( W! z) W, N0 J5 M# t" x6 k7 D
long t=System.currentTimeMillis()-t1;
" ?; n% [+ O! y$ n& z* m# R System.out.println("sum:"+sum+" time:"+t); ) t6 e" \- H8 X" a5 A
} catch (FileNotFoundException e) { ) y! q$ C* z4 f* Y; W1 D
// TODO Auto-generated catch block
4 b" @7 s- P" l& G/ K* F0 X e.printStackTrace();
9 V% |% t0 [& b4 D4 D } . F: w- X: g4 K5 c0 \, g1 K5 _
* j7 W, K" k; Q3 e. K; A7 J* I MappedByteBuffer buffer=null; b, y7 k; f$ H$ U
try { ' n; r- X% _ H4 z3 i4 f
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
% M- q- G! P* ^- [8 [3 Y: p int sum=0; " X" Z1 D& J! @" g+ p4 i, D
int n;
( a1 h$ f) c( s. b long t1=System.currentTimeMillis();
- V6 @3 W: N0 _6 u3 g& ? for(int i=0;i<1253244;i++){ ; t; T; ^2 r' O3 L" m& {
n=0x000000ff&buffer.get(i);
& d9 z5 q& g' {0 ^% H: m3 g2 S3 z sum+=n;
' U1 y% O% A0 k3 t+ K+ s# u }
, j" w* F4 Z H0 y9 @ long t=System.currentTimeMillis()-t1;
% d$ R& Q+ S9 b* e System.out.println("sum:"+sum+" time:"+t); " ]0 v7 e+ L) l9 X* K
} catch (FileNotFoundException e) { 2 K6 b. ~7 {$ [! R
// TODO Auto-generated catch block
) v- E: l( h, Q5 x) i5 K3 C e.printStackTrace(); # ^0 K3 u% w5 C" M6 ~' s: l
} catch (IOException e) { 7 K8 V. m/ O; d/ ?& G( Y! G& Q( P/ M
// TODO Auto-generated catch block ; G4 F4 i% k9 m3 ]4 l% X( h' W
e.printStackTrace(); , K+ ~% M1 b/ Q9 o
} , V1 f. m( K5 b: M
1 K- |" f: a% t. v4 n8 e$ g% K7 h! ^
}
7 ]! k! Q$ i5 E& \9 h8 `% ]3 G) ~
}
* u9 Z9 C* s9 ^9 f( k2 J! H测试文件为一个大小为1253244字节的文件。测试结果:, _3 a; k. R0 @8 R
# f' ~; `" l5 K0 K* ssum:220152087 time:1464 ; A6 N/ g3 R- D- z
sum:220152087 time:72 : R! o; N2 w6 p; p
sum:220152087 time:25
, J0 ?, m% P: `% M0 T# X" N8 h说明读数据无误。删去其中的数据处理部分。0 n# r# G: u) ]. p. ^+ j! G
+ ]+ M6 B& V L! ^' \package test; ' Z& m6 e% g/ r* K- d4 ?1 K
N8 g: P8 _' j' O
import java.io.BufferedInputStream;
5 O+ e# a# k: v$ t3 g# Vimport java.io.FileInputStream; ( ~( j2 @, N. R, u! ~1 x' a
import java.io.FileNotFoundException; 9 M; h' b0 x* v
import java.io.IOException; & i' E B3 e4 m# r& n
import java.io.RandomAccessFile;
/ G0 C/ X+ P+ W/ P- X: ?6 R7 {import java.nio.MappedByteBuffer;
8 R' t8 O7 |6 H5 F( {import java.nio.channels.FileChannel;
' x# g6 I, R6 [/ v
1 s% K9 S- J `$ xpublic class Test { 8 S( V- r( p! ~) n D
4 \$ T9 u! u' y K: x
3 v0 X& H* F% \. z( c$ V
public static void main(String[] args) {
% Y# m/ ~8 y; D1 y try {
! `* J! Z+ _/ i4 i/ ?" J; e! I: | FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); ) X* y; J" Z" K; F$ S, n! Y
int sum=0; " r+ W* L3 ]; A7 }" s0 v, @+ \( }
int n;
, l, @& y8 \" J$ R* h/ @4 Q long t1=System.currentTimeMillis();
4 `" N$ `/ B8 ~* w' K( q) A try {
' p0 P5 Q+ ~# l while((n=fis.read())>=0){ 2 \2 D% l# j2 o3 K
//sum+=n; ! `2 I# v& D; [3 W2 _% I
} " K2 N1 X" v' ~0 R; ~6 g7 @
} catch (IOException e) { ) R2 s! |" Y \9 ^! m1 Q
// TODO Auto-generated catch block 9 `5 R0 h T/ P4 B5 a3 b
e.printStackTrace(); & _% f. M: l% e/ @, V
}
! p8 o# n- k; y8 a9 a, m long t=System.currentTimeMillis()-t1; ) c. R# C: ~" t g# @# f& x
System.out.println("sum:"+sum+" time:"+t); + R! H& N0 U1 q1 e
} catch (FileNotFoundException e) {
( U/ U! c# s5 M4 m // TODO Auto-generated catch block
4 X1 ?8 Y7 r* m6 y8 |9 B e.printStackTrace(); 4 j% c, w5 v# x7 i; |# e. R0 D
} ( E# t# e' @# z! ~; J6 i5 Q+ E
) B0 n1 _. z2 `( D( T try { ' T) h' S) N, {* O2 U; j
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
; g% `7 z/ ]" W5 H; g9 l# X7 A BufferedInputStream bis=new BufferedInputStream(fis); 3 x+ G1 G7 X' N" [, Y5 Z5 f; a
int sum=0; ( Z% r7 D" O K" h
int n;
2 L! y/ ]" ~. V. U long t1=System.currentTimeMillis();
) P8 Y8 p6 X9 T" O! i6 G try {
% P# l5 C! v3 q G while((n=bis.read())>=0){
" H3 l6 [/ w4 o; H9 `9 X //sum+=n;
h ^) c( j# I N } / w; ]8 A5 K8 T5 s/ Q
} catch (IOException e) { 7 ~( f1 Y! A* a4 u$ V0 F
// TODO Auto-generated catch block - A: N) z" l2 {. p6 b) C( v
e.printStackTrace(); ; q# B" G0 j2 r& {5 |& w
} 2 h4 n. Z- C$ F
long t=System.currentTimeMillis()-t1; 9 b1 H. }$ A4 Q% I) j% R. ]& M1 ^1 _. i
System.out.println("sum:"+sum+" time:"+t);
; c) M6 u, R, r0 X } catch (FileNotFoundException e) {
/ T" Q0 O, G/ S // TODO Auto-generated catch block
- {4 p( B" S& M+ Q9 V e.printStackTrace(); 5 I" _' `4 Y9 n1 B7 C- i
}
# ~/ b( ~! J; s" p2 C$ H& w5 n$ ]: k/ J- b4 }2 g
MappedByteBuffer buffer=null; 6 B2 S; b0 k! U7 Y/ ]& Z4 T8 A6 z
try {
) ~- C, z' E6 q; E% R6 c buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
! I( x/ n& A1 } int sum=0; ; A4 y: B& `1 h/ Y; ?! r. j7 I
int n; 0 ^; ^5 G$ b7 Z ^6 ~3 t/ i
long t1=System.currentTimeMillis();
, E* e1 _ {/ A; m# h for(int i=0;i<1253244;i++){ k) L, x( J( a. a
//n=0x000000ff&buffer.get(i); & ^# o; `" o% T' V$ H2 m+ E
//sum+=n; 2 ]% z+ Q0 G; w7 i# x) q
}
8 b" @+ [& |" z5 i2 U* H, E; d+ K6 [& u& j long t=System.currentTimeMillis()-t1;
( D9 ?0 s% v: r. e4 K: a System.out.println("sum:"+sum+" time:"+t);
3 F8 i$ E$ v3 U1 ?! ?$ a: U } catch (FileNotFoundException e) {
$ A+ C' j2 M8 s2 { g // TODO Auto-generated catch block
! V) `, T9 \7 ~ S4 k" N e.printStackTrace();
4 c% M7 ?1 H; ^6 n9 b- C" a, d } catch (IOException e) {
, m% n8 {5 j" i8 j& t% o // TODO Auto-generated catch block 8 _ p5 }) ?7 Z# U% \, C
e.printStackTrace(); $ F% C0 N- M1 a t3 ~* y
} 7 o! n, b; T4 L% q
+ I6 |0 @2 w7 `; U1 T) ? }
. u! {& w. K4 x# y- N
5 l: X. ~4 j6 _% G}
' Z! ^ ?& V% v5 ?测试结果:
; T+ u& j$ ], |# {' x3 G1 R/ G4 z- I: G9 A2 Z- O3 C
sum:0 time:1458 3 o* l+ ^3 Q- C, k0 E
sum:0 time:67
1 n9 R) g! c6 ysum:0 time:8 & X! I# t: L. t- \: L' j
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
6 v1 i4 t" D- |" P: r" H7 S J9 O. S% L5 J
这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。
( K5 V- v a( j1 Q/ h& w" h% A2 W3 w: m% U) _/ g- [
|
|