该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
& }7 H8 v- l0 l+ ^/ ]& w: j$ ~3 t" d" M/ H
package test;
/ E! f' q2 K' _: \9 j5 L( _ o. C" f4 I5 @. C" Q3 ?9 A
import java.io.BufferedInputStream; " H/ f0 g- C: n' q
import java.io.FileInputStream;
& B+ }) a# J8 }- dimport java.io.FileNotFoundException;
, b1 T R; q) Jimport java.io.IOException;
, d$ z! h1 W9 l, I+ a% B+ E; zimport java.io.RandomAccessFile; 8 E. h8 F: i' x- L
import java.nio.MappedByteBuffer; 5 Q. h9 }# G$ J! K% ]7 g
import java.nio.channels.FileChannel;
9 P9 i2 H7 J$ {) Z( Q9 x( ^. k
8 K7 a7 ~- }: n( Q3 cpublic class Test { 6 i" p. W- d% z" [6 q
( a# Q# V# b6 T1 l1 n1 Y; |% x
4 c# P8 P! o! R
public static void main(String[] args) { 5 K3 G& u% P: r; R
try { 9 K( _2 R* `; v p
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
! n6 Q6 c' T2 p { int sum=0; 7 O4 j6 @: W; G7 E
int n; * k9 i3 f% Y. `, t3 ?
long t1=System.currentTimeMillis(); 4 B% r; u5 U. }9 z/ ?& O( U
try { , y' E, m5 n+ i) v y2 d: @# K
while((n=fis.read())>=0){ 5 y+ {% `' N- a8 Q5 L
sum+=n;
! R/ K3 O3 i( { } * E& A1 q P/ d* w
} catch (IOException e) { ( O7 I* X; g4 y" u
// TODO Auto-generated catch block % p- I, D2 ^. y8 p; G) ` {
e.printStackTrace(); - Q: ]0 \# o9 H
} ; W( T9 I* {3 }- E
long t=System.currentTimeMillis()-t1;
9 ]- L/ g/ e% V8 n9 p System.out.println("sum:"+sum+" time:"+t);
$ j! ~4 [, O. l( f5 r! O- b: G3 ` } catch (FileNotFoundException e) {
2 k" ~) f# y& z' n // TODO Auto-generated catch block % [- {+ j: r6 i) ~' k
e.printStackTrace();
: F0 o; r, I! K5 _$ Z! F9 L6 w0 k0 G }
2 U! V# _& S' S& F
% |+ {1 ~2 D% } try { + V5 Z4 _; @9 L/ f" }9 \
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
. t; s+ B3 t* ~; q BufferedInputStream bis=new BufferedInputStream(fis);
" {3 r/ _$ c% B6 D+ j ?( S; [! W int sum=0;
- z( ^4 E1 X' C8 n int n; * s6 I2 o/ K2 d" M. ^
long t1=System.currentTimeMillis();
9 L# h) M2 g* S) K try {
2 k, c7 h% E# @4 W# }6 k) f while((n=bis.read())>=0){ ' e8 z! |4 m2 _, e+ _% h% u5 e& z
sum+=n; 6 t. H8 d* A3 Z% H
}
5 F$ j) n5 `) w/ h4 _5 I8 Y- P2 Y } catch (IOException e) {
$ F4 B# P& G! M4 S9 @1 D // TODO Auto-generated catch block ' I1 A* V( c9 x" P! e- P( s
e.printStackTrace(); / j p9 q, H5 c) m& X$ k3 Y
}
; ~, o4 o: h, t long t=System.currentTimeMillis()-t1; ; V7 U: l4 P V
System.out.println("sum:"+sum+" time:"+t);
" w7 x7 z! o3 {& V: X4 z } catch (FileNotFoundException e) {
& @( X$ m" w$ |# i/ g E4 q' V // TODO Auto-generated catch block . B( s( b! p$ C: ~& \- k
e.printStackTrace();
- I0 e: Z+ }2 g' b } ' `' V, N% O! b4 q' N
- |! O, Y1 e/ |. V4 t; T1 M; r
MappedByteBuffer buffer=null;
& L; r: _9 _6 {% x5 s; f6 h try { 8 u# b6 u5 r& D
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
! o& k J. u! o$ R. g int sum=0;
; |* v' m' _0 p* |4 ?& A4 B; d% e. P int n;
8 D, V/ |/ p0 |9 v0 Y* i long t1=System.currentTimeMillis(); " D- q% ^* n; R/ ?. _: b9 L0 h
for(int i=0;i<1253244;i++){ . I% j* S+ R! R2 b, h
n=0x000000ff&buffer.get(i);
, y& ?& t+ J: b$ n sum+=n;
. S% l) Q. z) z& i3 T" B }
- Y% K" k: o: A6 f; f& | long t=System.currentTimeMillis()-t1;
/ ~; ]( I X/ I$ Z2 d" z System.out.println("sum:"+sum+" time:"+t); 3 O$ w) i3 I" o9 i) C
} catch (FileNotFoundException e) { + n4 O$ N7 l1 B9 ?$ | N: g: @1 F
// TODO Auto-generated catch block % ]0 S7 R8 f4 A# J1 Q3 U6 g% \
e.printStackTrace(); 1 K H ^ t. R8 w
} catch (IOException e) {
4 j' X9 D+ Y2 m // TODO Auto-generated catch block
* V: q+ G8 R3 C0 e0 D4 B B e.printStackTrace(); 4 A' \2 k5 E t: d Z
} 5 n9 h$ x1 w# h6 z0 m# r( ~- z7 Z
# `" n9 w4 l9 C2 J2 d } & H6 s# {! L& H7 S
P! ]* q3 E0 U6 }7 {2 b% I: `}
E. t0 u3 j/ e5 s测试文件为一个大小为1253244字节的文件。测试结果:# N8 _2 Z( X1 b
" g8 F! [* f- w- \+ a" p7 ^) Xsum:220152087 time:1464 1 B# c$ k. d$ p4 H4 ]3 @' D* F) S- F
sum:220152087 time:72 ) O0 C5 r( Q" k' x* e" P2 M
sum:220152087 time:25 # w4 U7 l9 c* V4 v( o, b
说明读数据无误。删去其中的数据处理部分。( t1 V/ D* T- a% N+ n" X7 N
" z. X8 b' h. v6 K7 j$ wpackage test;
, R+ T( b& ]8 O9 Q
% v- S+ ^& ^+ R z4 H& W2 W; ~import java.io.BufferedInputStream;
) ]6 i0 N. w7 z& d4 @import java.io.FileInputStream;
' C+ g$ n7 }4 p/ _) Z, q7 z# Himport java.io.FileNotFoundException; 6 n& ^/ G. M( q; y5 y6 S" z6 V
import java.io.IOException;
8 Z4 ]$ l8 K8 ?0 simport java.io.RandomAccessFile; # g& }" w5 O* s; A* g" b
import java.nio.MappedByteBuffer; . ]& ^% v/ A' q$ i" e
import java.nio.channels.FileChannel; 6 T* \8 |2 ]) L2 K
/ V8 K9 Y' J. S' v
public class Test {
0 s M& r. h Q8 E0 _7 r* M4 U& i' G, S! n8 t8 E
: F0 g6 Z% u: Q2 l
public static void main(String[] args) { 9 x8 @- A8 R) c: w
try { 8 Q& I/ W% A. H7 i- m
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
4 p3 m' ^: D7 u) P: _ int sum=0; ) r4 `- T" Z F3 r! M. n3 c
int n; ) ] A& u3 l: g$ G" w
long t1=System.currentTimeMillis();
0 h) S$ H2 q, g6 r7 ~% D9 s2 L try { : l. c) k+ _' o8 o& v$ D H
while((n=fis.read())>=0){
' t/ _* g) E/ b //sum+=n; 7 _# I5 T) f6 `5 E
}
. r' ~7 M: ^8 p } catch (IOException e) { " A2 k5 g" O2 W2 e1 m# J9 w" l* o
// TODO Auto-generated catch block
# a; D7 k1 O- F# j, c! h e.printStackTrace(); 2 Q: n0 Y7 r) T4 j
} + L, H, _) e/ j9 {2 s
long t=System.currentTimeMillis()-t1; * J4 I* u4 D( Y* q2 W1 g9 f5 t
System.out.println("sum:"+sum+" time:"+t); 1 j0 L! ^! g! `* h
} catch (FileNotFoundException e) {
7 _( }% e: Z0 v. R // TODO Auto-generated catch block + C; a5 z" u- M z& L( ?
e.printStackTrace();
3 \# o( `) ]0 f, I5 n* y% R }
2 A+ {6 d- O/ S# e+ B1 R3 @/ ^; s3 q9 M
try {
7 q+ \7 N, ?! s+ n" C% { FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
) q6 ^7 G9 y3 a: ?" O BufferedInputStream bis=new BufferedInputStream(fis);
" M: U! f& y" r4 s$ A" s int sum=0; , M% a* y2 c# G. ^% q; J8 x0 _
int n; 5 ?! l, g- P8 A
long t1=System.currentTimeMillis(); ! e" R1 j$ Q/ S8 S8 Q# E1 E
try {
# M; e- `. [5 Q# E while((n=bis.read())>=0){
, x9 I( M" ~2 e" e //sum+=n;
/ {) N( J0 u6 Q }
7 w9 \2 @3 W6 ? } catch (IOException e) { 4 @5 G' j% b( H6 a
// TODO Auto-generated catch block & p; [8 E5 I/ s- e6 I- \% G
e.printStackTrace(); ; k/ A, B5 e \( ~. g7 J. Z
}
. X; X3 f! d; w long t=System.currentTimeMillis()-t1; ' I: A& X; d, ~- [" {
System.out.println("sum:"+sum+" time:"+t); 0 w" }7 L) x! m' }* @3 K
} catch (FileNotFoundException e) { Z3 h9 T. Z* H$ u1 Y4 S8 z
// TODO Auto-generated catch block
1 v" p# B0 N, X% v6 j e.printStackTrace(); 7 S. ^. v b, d0 V9 x
}
5 C$ Q- }$ H U, i
! B$ T, D+ l, Q# q5 H6 j2 W# m MappedByteBuffer buffer=null; ( L' N* [. z5 c$ w( G
try { 8 W7 ^$ S. y5 d! D$ S9 v8 j
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 8 i5 k. g7 Z8 ?) f" B+ x
int sum=0;
- N! c$ P8 r2 V+ B8 q int n;
) }3 }# m, O6 }% {4 _3 k long t1=System.currentTimeMillis(); $ d+ X: L. Z* q! a
for(int i=0;i<1253244;i++){
# S% ]- A4 H8 i //n=0x000000ff&buffer.get(i); ; [1 r! p2 K% S0 i; D! Z& t9 F. K
//sum+=n; ( ?# X$ s% b% J3 [" J/ v
}
$ s. \' v4 ]8 m3 ~- ?( D" c long t=System.currentTimeMillis()-t1; ; D) S7 X8 H' F, [1 O. Z1 H" s7 R- H
System.out.println("sum:"+sum+" time:"+t); ) a. M- g: U3 {4 M/ y$ r
} catch (FileNotFoundException e) {
r$ { n+ P6 [/ a // TODO Auto-generated catch block 1 `+ v! ?. w$ [ @2 E3 p
e.printStackTrace();
- l# t' Y. F, c. _ } catch (IOException e) { ) ~/ s6 t, Y8 {9 j
// TODO Auto-generated catch block
: \0 |- ]' p; _, Q; n/ U* u2 @! f+ t5 Z4 G e.printStackTrace(); ) A! l3 E, r2 c2 R( }, j' B
}
% {4 v+ i1 P! J8 Y/ ~9 i X; Z* j4 Y `% I* d5 |
}
% {9 |! n4 X- O, m* F
+ m+ m$ I, A( G4 }- u} & b9 }+ m9 N3 v6 H+ c, V
测试结果:
: T7 x- g9 l& a5 z- ~# n+ k
+ A1 f+ @/ @) Nsum:0 time:1458
( S8 V' u2 P7 T, J" fsum:0 time:67 % `; U+ N) ^3 H# z2 L* n
sum:0 time:8 6 e' P4 F+ w. n/ F9 x+ s R' \- r
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
: k: J" C: {* X, a/ D* [
) r( S3 t- d; F" |+ J% k+ R这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。
$ @; `* b. h2 k
1 \$ v, t: _$ v, k. k |
|