|
该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。
( z& v% O, U1 n: G
5 V( E1 i( L) ^5 ypackage test;
) r, ?( ?1 g, M: R+ c1 B+ e% J4 ~0 s4 O4 W! x
import java.io.BufferedInputStream;
0 p, R# [/ q6 m% k! u; X& Mimport java.io.FileInputStream; ! {7 i$ l* U# t$ I' L
import java.io.FileNotFoundException; 2 K* }( v, [ Z3 l$ m0 X9 w, u
import java.io.IOException;
8 d) i2 [- V J2 a# R$ l6 `import java.io.RandomAccessFile; ' p+ ?6 j y# d6 y) g
import java.nio.MappedByteBuffer;
1 ^' g9 A6 `9 L; Oimport java.nio.channels.FileChannel; ) |3 p" b" q0 h# ]
" P- c8 f# r2 d4 Mpublic class Test {
! S" s4 m1 k: z
/ I' k7 X% A. M# ]! C6 Y K7 h- w; x' {& L/ W' O. _
public static void main(String[] args) { * {$ K( I! Q+ R$ I5 I8 N6 |$ l
try {
9 X. @$ W6 F+ s L- V3 w" G FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 0 o# X- B, o0 Q* @5 |
int sum=0; : e# w" a! e* Y% z
int n;
* c/ B5 ^7 o& l) F& M4 ? long t1=System.currentTimeMillis();
/ e; P& Y$ {& v3 h: { V try {
" a2 F5 R* l2 T/ u while((n=fis.read())>=0){
$ o' b* P" F, o" ]3 L B sum+=n;
0 i3 C% v; c( Q% T% @; Q6 A }
2 S( U1 r s' S+ y5 _9 `: ~ } catch (IOException e) {
$ Q0 X8 `! Z/ v7 a& U2 ^1 `; N0 _ // TODO Auto-generated catch block
- o0 G0 Z( J1 l9 j e.printStackTrace();
4 \* Q: A# P5 }: f } 8 e5 l& K. d6 G9 X: `% n
long t=System.currentTimeMillis()-t1; Q% w' c0 O& t' p3 \4 |
System.out.println("sum:"+sum+" time:"+t);
( E9 E$ G4 ]$ q& U1 q% g } catch (FileNotFoundException e) {
7 m1 a9 S0 m, J // TODO Auto-generated catch block
% l6 a4 g( f% J" A2 t e.printStackTrace();
. g( @0 |2 [( O, u) R. z } h" K- V2 f3 P4 |/ E: M
8 r) h% X$ p% G. O try { # }! P! {# h7 [2 s0 h1 G0 b
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
; d- Z( G+ J& J) Q5 ^# l8 f7 k/ | BufferedInputStream bis=new BufferedInputStream(fis); ; n( E9 u l* T! ?/ K2 h
int sum=0; - Q8 F, I" o' }% a& m
int n;
* K4 p( M3 G8 {) h2 @ long t1=System.currentTimeMillis();
$ H3 ]! g2 J* Z* v* Y try { ! Q5 z k9 |9 j. R% Q$ h$ k! E
while((n=bis.read())>=0){
9 F6 |. Z; d' x# f& ^0 Y Q sum+=n; + D/ w$ T3 _' t) _% k1 C
}
2 o7 T. A1 Z: K) R4 K } catch (IOException e) {
/ i4 r9 g. \: }* Z: S' R% v0 r4 u0 W a // TODO Auto-generated catch block , k- C- m# Q: [+ l" g8 ` P) a% P
e.printStackTrace(); ; n7 E& m, e4 Z5 G. D. `! @& Q3 w
}
5 O! \; k" `7 B: X- }$ u long t=System.currentTimeMillis()-t1;
* [/ g( o' j. ~# F+ ^ System.out.println("sum:"+sum+" time:"+t); 2 g$ a, U) I; s
} catch (FileNotFoundException e) {
U/ H4 u& T0 S // TODO Auto-generated catch block
: _% Y/ {* n F% k, `- P e.printStackTrace(); 5 }2 T6 w6 d( L* i: F) s
} % O5 t5 w/ E4 P/ K
; O8 p$ ] t5 T. A s
MappedByteBuffer buffer=null;
0 { C7 q. } X" O% f4 x try { 3 P; u6 C% O" [% I6 |* H: G7 m
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); ' \* n }- S% u
int sum=0; 3 J4 H7 _6 N: N" y. T, l
int n;
7 j5 ]. Y4 A- [) r, J long t1=System.currentTimeMillis();
1 l6 ~0 x/ `7 `4 G; U5 H% x for(int i=0;i<1253244;i++){
; G& F, d8 k% \) C U7 r n=0x000000ff&buffer.get(i); , \+ O, [; V2 Q, [
sum+=n; % K% Y# T1 ~" A7 q- o" a- I
}
7 J- h; M+ l6 S# H: f+ h long t=System.currentTimeMillis()-t1;
7 k( K( B0 ^( K) p System.out.println("sum:"+sum+" time:"+t); * Z% } r' X: `4 M' w, g
} catch (FileNotFoundException e) { % M, r! Z8 A9 A
// TODO Auto-generated catch block 7 h. }3 u7 A# o* M/ e# I/ A
e.printStackTrace();
u( B3 w( }* ]% r( L$ K d7 q } catch (IOException e) {
+ ^5 s: x: x- J* A* X // TODO Auto-generated catch block
j: N Y a; d1 S e.printStackTrace(); $ y9 T7 s( t0 P$ s) e
} 7 P3 U; j- E3 ?6 H* v
. |% J0 p( R. ?/ e! A. W( ?3 s } 0 W' A; w- u) r- F
' Y5 u- |# b, Z; x# ~
}
0 i+ W3 p& k( F3 H6 I测试文件为一个大小为1253244字节的文件。测试结果:% T" } f1 _$ A6 a
& O) J" j/ N/ K, J+ t" J% o m
sum:220152087 time:1464 / N! @& T. z) J% m% p
sum:220152087 time:72
% f: H L! h) V- V" csum:220152087 time:25 + p. ?( P! L; ?" b# @* T
说明读数据无误。删去其中的数据处理部分。# q' D4 t; J" e
: y2 N- G1 }7 R9 \package test;
8 ~) [" b- y9 U8 o: U6 O, R5 W
3 m; p: k+ y+ }. h: [/ ~import java.io.BufferedInputStream; : M& L* x+ f& s' T0 {
import java.io.FileInputStream;
% ^4 k z. e5 y# Zimport java.io.FileNotFoundException; & n' q" P5 t0 U; \. K, V
import java.io.IOException; , L, n* ?' z2 h0 B/ l5 n
import java.io.RandomAccessFile; 5 `7 k S, b J* ^
import java.nio.MappedByteBuffer;
! B- G4 M0 C/ timport java.nio.channels.FileChannel;
$ P+ G' H; c& j' v/ |; n+ V' Q3 R8 H3 p! z) K
public class Test { + f% ]' [; P, [- c" t7 X
: c7 L* I1 \* S" A b" S
5 |5 Q, ~+ k2 S+ m* ? y* c public static void main(String[] args) { 1 y5 U% A- M2 x' G
try {
; f' A4 `+ i' E" U# K/ l2 y( x6 | FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
9 j1 ]2 }5 ~$ D. r3 @; H3 G int sum=0;
+ g% f" ], N+ n" f( S& A' N int n; ( u( J. q2 [- Z6 S. F% {
long t1=System.currentTimeMillis();
+ C. u# f4 `0 g5 Q0 o try {
* m. ~6 V- Q6 D! W: a3 I6 e while((n=fis.read())>=0){
3 r- J$ p8 w* G4 P7 t //sum+=n; % n4 Y/ M4 w! A9 r! v" G' R
} * E$ t/ I( E9 w! Z9 A$ s
} catch (IOException e) { ! D' Q4 t5 d# S4 v, \& D
// TODO Auto-generated catch block
" N( o- F) C( I, n. M e.printStackTrace(); 6 u2 Q% C, [9 z: B$ K* k
} * u0 A& z0 A" X6 R3 K9 U1 J) p. t
long t=System.currentTimeMillis()-t1;
% N) F, T! c+ L, m; o System.out.println("sum:"+sum+" time:"+t);
4 ?* q( x P3 t6 z' Q2 R1 D } catch (FileNotFoundException e) {
) z5 u. {8 _8 L) D2 U9 X // TODO Auto-generated catch block 1 m2 I0 }) D- q; X% L
e.printStackTrace(); 1 w1 ?1 M7 B/ T! h+ N; H; b
}
: ?( r- P, V. B6 V W# V( [7 R3 e. t2 L, p) X# o
try { ; l* @( K! x# C! y/ d% }, o0 J
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
5 D9 L. Z- T- I9 r BufferedInputStream bis=new BufferedInputStream(fis);
2 T# C- K; X( ` N int sum=0; ' _% H5 c5 q8 c/ I8 e# x1 _9 T" }5 _
int n; 5 s# ?5 g2 P1 a
long t1=System.currentTimeMillis();
2 K% k4 S# Q9 j6 [. o2 ]& g# @ try {
. l& B- ?. D$ s; e2 n while((n=bis.read())>=0){
/ j: N+ W/ y, R. |- ] //sum+=n;
8 L; ~. v/ ?3 `. l `' ~ } 0 _- k: X* b1 ~4 s
} catch (IOException e) {
" W' p. i7 h5 a3 ]6 V // TODO Auto-generated catch block
" z- i/ g. \5 t0 n+ X& W e.printStackTrace();
. r( j/ }1 i, R. s& T3 u }
- d1 x) V; Z; G long t=System.currentTimeMillis()-t1;
) ?" L. g( S2 n System.out.println("sum:"+sum+" time:"+t);
9 R- x" k) V# t: A/ Y/ C } catch (FileNotFoundException e) {
# {3 ]$ E4 X8 @/ u: m% U // TODO Auto-generated catch block " ~$ R) p: M" T, z% j! c3 f y
e.printStackTrace();
8 ^2 m2 U# I) H6 V9 ~ } , g6 k" G0 \ g/ B! n
+ O" O! Q* ]" a3 O MappedByteBuffer buffer=null; ( u" E! c$ [; I. a3 {9 q
try {
$ l/ e. S# G- h% R buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
% z/ C- `! X% s; m5 l int sum=0; * l; |6 k, S! B- ~) ? X' @
int n;
B1 ~7 ?1 c" o" q7 X long t1=System.currentTimeMillis(); * g$ c o, }7 ]$ J4 w( n* f
for(int i=0;i<1253244;i++){ * [2 t- V% o9 m1 i) {2 u
//n=0x000000ff&buffer.get(i);
$ j! x0 K/ i, k) A4 I6 O* { //sum+=n;
' b# X/ E# P" J* j$ v2 z } 7 H$ y# l7 ]) b# g. k
long t=System.currentTimeMillis()-t1; ( _# P6 ` u7 w" G3 s6 E5 k
System.out.println("sum:"+sum+" time:"+t);
! [' a2 D+ i/ \2 z( N! U2 ?+ c' d% U } catch (FileNotFoundException e) { 3 N2 u; o; e; j$ ?# m# T, l+ q+ J
// TODO Auto-generated catch block 4 N4 B4 P+ S! `
e.printStackTrace();
+ M% J+ v' N) R } catch (IOException e) {
# V% [* A, X% \1 k5 X" M/ l // TODO Auto-generated catch block : Q. O4 i' g# `9 E8 j, ?
e.printStackTrace();
6 z) F5 I' Z1 {' e } - @8 @" b# Q/ D( |$ r
3 H, v! l' l& P- h! ^
}
2 m) F" T1 E# c3 X0 n$ Z+ [. b4 z
; t5 [5 ]! p& k3 v7 w1 m}
/ K. A7 H% X V7 \3 @; a: l+ n测试结果:- A& I+ Q: z: }/ F; {
( I7 t" y! u$ C+ y
sum:0 time:1458 3 m7 a; Q' d3 ^" I1 x; x
sum:0 time:67
; F8 T5 g& F% ^: A4 Xsum:0 time:8 $ q$ E! h/ ~" S0 m: F# |% `2 X' [9 K
由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。6 S: d; Y- P& O9 ^
4 v& p) E3 \4 W& U8 D这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。8 O, I1 w7 B& O% S6 k
9 a2 S, g+ q; V# p" q5 { |
|