该用户从未签到
|
在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。. w1 r8 Z+ g2 Z- P
( Y1 J4 k2 t/ c) v3 o
package test;
& g$ x8 G7 m) y7 `0 F! b8 T. n5 Y5 K) e. r8 i: S' a" _
import java.io.BufferedInputStream; , I \ [8 b, G4 Q6 i& ?
import java.io.FileInputStream;
}" o( K/ e/ B. j5 ~$ `. Jimport java.io.FileNotFoundException; 9 [ ?# }/ @0 I
import java.io.IOException; 8 _/ [$ `. n, c" ]: S1 L
import java.io.RandomAccessFile;
9 t- v: A+ v+ Eimport java.nio.MappedByteBuffer;
, B: } u$ w' D5 v" u' Mimport java.nio.channels.FileChannel;
2 b B( q, i. s( V6 Z M- @1 b( c* l" F7 a; R* ~3 y5 f4 {5 S
public class Test {
& r( j2 _1 p4 ?
8 C. `; q8 z/ q7 `& j" U& x: k6 |% y8 h- N$ E: u/ w
public static void main(String[] args) { 7 I+ v8 c8 d2 |' @1 m" I
try { / j1 Z( [ ~6 [: e' R( R
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
* X. s0 \$ j: K4 H/ s2 y int sum=0; + Z# C* i3 j) \& O3 q
int n;
/ F! h% s. N, p" [ long t1=System.currentTimeMillis(); 1 I2 O( D: q" n% q
try {
1 l" |/ J! p+ a3 K1 L while((n=fis.read())>=0){
, ?+ u8 F [( @1 I sum+=n; ; A' Y3 K; D7 U q1 _4 c
} 3 `, L; p) F! R# Q Q
} catch (IOException e) { 5 e0 b2 C( D& y" K+ L
// TODO Auto-generated catch block + N: s3 Z; R" q6 l7 k
e.printStackTrace(); # ~3 ~0 O( M9 }( X4 I
}
* { @% z5 O. \& D: S9 W7 C: z long t=System.currentTimeMillis()-t1; ! s# \$ Z2 s0 q3 e+ \
System.out.println("sum:"+sum+" time:"+t); 1 r9 i ^) \( A9 t! z x
} catch (FileNotFoundException e) { 1 S5 V, T9 o2 R' R2 P2 u
// TODO Auto-generated catch block
# b+ B+ j- L7 N1 n5 M6 x e.printStackTrace(); - M$ i$ U: E; R% U& I
} % n! T( Z7 W# f+ k+ C& Y
! t/ r/ D3 z# ~8 V) @ try {
$ [+ g4 D" }2 r1 L8 Z+ E FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); / Y7 x& h* M: ]; B! X _
BufferedInputStream bis=new BufferedInputStream(fis); " h' C* f/ c7 Y1 e# r
int sum=0; ( z" t, [4 ^ { n+ J; G
int n;
$ h! F/ {! T6 k9 q- `# z long t1=System.currentTimeMillis(); ' U- X! I( j0 Q! n8 z) ]
try {
! L3 I/ f6 l& H z4 Z2 p while((n=bis.read())>=0){
# p# D2 b; U& ` sum+=n;
5 T/ K$ l/ M4 _6 A$ a }
1 w4 x0 u$ r- m } catch (IOException e) {
8 J' V/ n N- ~, m // TODO Auto-generated catch block
' `6 ?5 h/ x: a+ @, E e.printStackTrace();
' E% a% g- V1 k; C( p @( M0 Q0 t }
F2 V% j/ t$ u2 ]3 `8 M& F long t=System.currentTimeMillis()-t1;
6 K9 {: g2 U4 C8 b. n System.out.println("sum:"+sum+" time:"+t);
/ O8 f8 z, \( ~) E# A: x" C3 m( y } catch (FileNotFoundException e) { 9 v0 K) t$ d) ]
// TODO Auto-generated catch block
5 c6 P: G8 v; m+ A7 [ e.printStackTrace(); ; H* |( o- E& p- P& o% F p
}
, r5 G; _5 v$ }
. H2 }6 o; O5 E. q! l: W MappedByteBuffer buffer=null;
2 c& [2 w0 x/ j, G try {
$ G* P, A. v- @0 x. s8 Z buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
5 E5 P, f, K i6 u4 F, L( ` int sum=0; 6 h" a% k8 j( T U; F3 J1 n
int n; + {6 [4 a2 y$ E H! u g9 C/ A
long t1=System.currentTimeMillis();
9 c& D% e& _' y3 r* J, `0 c for(int i=0;i<1253244;i++){ 8 e* Y- I4 r7 B
n=0x000000ff&buffer.get(i); * m" @& Z0 _; s7 }
sum+=n; ! i; c7 h: a8 k6 u& A3 P
}
3 U3 _3 Z" M0 U9 R, u0 |; b" J long t=System.currentTimeMillis()-t1; : s' m7 Z& {. B, P6 E. W, |
System.out.println("sum:"+sum+" time:"+t);
6 @+ x* r6 R; ?% n } catch (FileNotFoundException e) { 9 Y+ f) a1 x5 Z6 {% X* N6 p
// TODO Auto-generated catch block
2 \1 H3 P7 b+ a7 u3 y# D e.printStackTrace(); % ^4 a& e* n" l: D. v, h) h2 O
} catch (IOException e) {
4 T& p0 G1 O( u# S& R: b2 t) P3 @ // TODO Auto-generated catch block $ D8 ?& z* ?2 @& [- D
e.printStackTrace(); 5 l2 k7 [1 x0 @
}
& Z+ A+ u# `$ F+ N2 I' d2 h
" N$ i p C$ J3 M$ a7 H t0 a }
# q: t7 m- p, x3 M0 x/ m0 e6 Z, \5 [' _9 `- J
} 1 m% g$ d0 }+ @! K
测试文件为一个大小为1253244字节的文件。测试结果:
5 P9 Q+ p$ b$ c$ M; a6 w
: ?& M7 e* V9 W5 o% Jsum:220152087 time:1464
4 O) y( H- W, ^) R3 lsum:220152087 time:72
5 x1 {2 ~ B5 R: Isum:220152087 time:25 $ L& {( w, m3 V
说明读数据无误。删去其中的数据处理部分。
; z, B; D% [, K! w. L
5 q3 ~0 e, c, y7 i8 U5 v: Jpackage test; 8 T4 J% u( V: q0 {: _
7 c: T. M7 T; J
import java.io.BufferedInputStream;
0 T0 D/ F' i: p1 jimport java.io.FileInputStream;
! I# o% E6 X1 B- timport java.io.FileNotFoundException; 5 n+ d8 S- m) G( h
import java.io.IOException; # @. h( ~3 f; V" l. i5 t, {+ L
import java.io.RandomAccessFile; ' a( |; O) g s$ b6 L2 U# ~
import java.nio.MappedByteBuffer; $ ^9 g- S' L) n0 q* ^
import java.nio.channels.FileChannel; $ `, Q8 Q* p) A! I- W8 ?" ?1 t8 X: E
0 T1 r" K. }# Z ?) b& e% |2 N
public class Test {
4 y+ t; e% _" |( v9 ~( f s- }; h" w( }& Y1 a7 r/ X& `" g& Q
9 s' p. { X V" [: m% | public static void main(String[] args) { $ w/ y5 D% H& i4 w5 n+ r& [
try { ) Q- ^9 ]% {, ^& e# O, ?
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 2 |" u- ?3 J8 z' D" V ~9 S
int sum=0; + o- `, q* }# Z' \2 [! E1 Y# a
int n;
+ F$ B( p& v4 }1 I" W" M% L long t1=System.currentTimeMillis();
$ s- f: i5 l2 I: Y9 h try { ; k1 s9 Z1 n6 f. I. {
while((n=fis.read())>=0){ 0 b0 _' l% P; B0 `2 l. z
//sum+=n; ! b# @7 u( A4 V: b- J
} 3 r" x/ X- \& @: d
} catch (IOException e) { ! N$ v' i% k9 f8 E" ~5 `+ c" ~0 A5 n
// TODO Auto-generated catch block 7 \# P# ~0 B% M" ?+ j
e.printStackTrace(); - Z5 |2 T! j, o, d3 f& `
}
4 U- p! _5 l+ G. y1 a long t=System.currentTimeMillis()-t1;
V* b7 o- }% j0 L/ X$ G$ \0 ^$ H System.out.println("sum:"+sum+" time:"+t);
7 {& m) ~6 M, D7 v! V } catch (FileNotFoundException e) { 2 m; y2 o! A/ H3 h2 R
// TODO Auto-generated catch block
$ `4 A1 }9 ~3 Q4 |; V& ^. W e.printStackTrace();
+ q8 j& u" f- ^6 f6 D7 C8 x8 y } * C- U" v! f# z. D) s& K* F! V
% u0 g! p9 g4 }$ ?
try { d8 L" s6 ~% k' B! Q
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
4 |0 A. d1 F/ S5 X1 I BufferedInputStream bis=new BufferedInputStream(fis); ) ^, p% S5 h5 [5 L! o' J. Z
int sum=0;
) L: M- \* n6 e/ t int n; + s9 X/ m/ m9 `
long t1=System.currentTimeMillis();
8 C' h* K. F5 N' U- ? try { ; K) h) H: @; o& q3 K) M
while((n=bis.read())>=0){
3 t8 i, w4 U+ k* n/ o //sum+=n; 8 [6 g# P& V; e
}
3 i* K4 U7 T) c. N p, h" q } catch (IOException e) {
9 n: p1 K; k$ |9 V+ O // TODO Auto-generated catch block
% T( n. M: B& F6 a$ {# C e.printStackTrace();
. q8 |8 `# |$ _ }
) I& e$ t/ G" H* S |# I5 R8 c" i B long t=System.currentTimeMillis()-t1;
( Z7 _8 J4 B' a: G4 U/ K System.out.println("sum:"+sum+" time:"+t); 3 R+ u9 R* u! m7 C
} catch (FileNotFoundException e) {
% B1 I. r0 X7 u! @; ] // TODO Auto-generated catch block 2 [4 | p' \7 U+ C( e
e.printStackTrace();
" l. z) l6 r2 u( s- U% H# j) T( [7 x }
( [5 S( K* `$ g' j! |+ x- Y9 s5 [( Q x- k
MappedByteBuffer buffer=null;
- T$ J b. d _$ p8 M" f" t try {
1 b' ^ k9 h8 q* ^, U buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); : r) ?3 A6 F3 [0 F3 F, z9 {' z* u1 y
int sum=0;
3 M) z/ E! p" m7 `4 L7 L int n;
5 s7 Z# r# g- Q5 g; q2 H; K C long t1=System.currentTimeMillis(); 7 x d r8 _; |4 d
for(int i=0;i<1253244;i++){ . A: g8 H' p' k
//n=0x000000ff&buffer.get(i);
: P& M& f" P7 r# D: T$ q' X3 w //sum+=n; # w9 q4 ]" P& O$ r w1 X# T% Y0 M
}
# _0 C' E' D$ _; h# V/ S9 U! G long t=System.currentTimeMillis()-t1;
( ]; J* Q. L1 g |1 z System.out.println("sum:"+sum+" time:"+t); " e- L' R" A0 ? S
} catch (FileNotFoundException e) { 3 W* K) ]$ {0 p% O3 M
// TODO Auto-generated catch block ) {$ o8 s8 T& J
e.printStackTrace(); 7 K/ t- ]: W) X J5 j7 e
} catch (IOException e) {
; V2 V6 ?0 C( g" r5 A1 m // TODO Auto-generated catch block # x7 l9 R- h8 z% h" l
e.printStackTrace(); 4 z* L* _! J+ B9 g0 d5 s6 ]( f
}
7 h7 f' B; l+ ?, ~& N* g8 N% v6 B" K. k$ `' F8 d) T
}
$ O4 s: H. |# f1 o
9 I* I7 K, j9 r4 T; S} 8 p0 g9 k& p8 E
测试结果:' ]7 a6 g% U6 Y: t2 `' c
% c, P& g8 E9 j$ M! z0 z# [% bsum:0 time:1458
9 v3 O) [( u+ o$ Ysum:0 time:67
0 v/ {2 X7 J+ m0 b; isum:0 time:8
. ~/ S! m/ g& A/ l) d由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。
2 u/ T! A* o8 `: v9 I, A
, U; }1 l+ S# d8 B这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。1 `' \) L: P5 T3 j6 M5 q
+ y( L# Z- D) _$ R& g/ x, h3 E |
|