package org.cholwich.binvstxt; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Random; public class BinVsTxt { public void writeTextFile(String fname, int N) { try { Random r = new Random(); r.setSeed(10241024); PrintWriter out = new PrintWriter(new FileWriter(fname)); for(int i=0; i<N; i++) { out.println(r.nextDouble()); } out.close(); } catch (IOException e) { e.printStackTrace(); } } public void writeBinaryFile(String fname, int N) { try { Random r = new Random(); r.setSeed(10241024); DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(fname))); for(int i=0; i<N; i++) { out.writeDouble(r.nextDouble()); } out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public List<Double> readTextFile(String fname) { List<Double> l = new ArrayList<Double>(); try { BufferedReader in = new BufferedReader( new FileReader(fname)); double d; String buf; while((buf = in.readLine()) != null) { d = Double.parseDouble(buf); l.add(d); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return l; } public List<Double> readBinaryFile(String fname) { List<Double> l = new ArrayList<Double>(); DataInputStream in = null; try { in = new DataInputStream( new BufferedInputStream( new FileInputStream(fname))); double d; while(true) { d = in.readDouble(); l.add(d); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (EOFException e) { try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return l; } public static void main(String[] args) { boolean read = true; boolean bin = true; final int N = 10000000; for(String s : args) { if (s.equals("write")) { read = false; } else if (s.equals("text")) { bin = false; } } BinVsTxt m = new BinVsTxt(); long start = System.currentTimeMillis(); if (read) { if (bin) { List<Double> l = m.readBinaryFile("out"+N+".dat"); System.out.println(l.get(N-1)); } else { List<Double> l = m.readTextFile("out"+N+".txt"); System.out.println(l.get(N-1)); } } else { if (bin) { m.writeBinaryFile("out"+N+".dat", N); } else { m.writeTextFile("out"+N+".txt", N); } } long stop = System.currentTimeMillis(); long len = stop - start; System.out.println("Required time = " + len); } }
เมื่อลองรันทั้งสี่แบบดูแล้ว ปรากฎว่าความเร็วที่ได้คือ
- เขียน text file ใช้เวลา 6.109 วินาที
- อ่าน text file ใช้เวลา 11.666 วินาที
- เขียน binary file ใช้เวลา 0.583 วินาที
- อ่าน binary file ใช้เวลา 3.698 วินาที