Advertisement
Guest User

Untitled

a guest
Dec 5th, 2010
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. import java.util.Random;
  5.  
  6. public class TestWrite {
  7.     interface IntWriter {
  8.         void write(int[] ints);
  9.     }
  10.  
  11.     public static final int NUM_INTS = 1000000;
  12.     public static final int NUM_TESTS = 3;
  13.     public static final String[] TESTS = { "Buffered DataOutputStream",
  14.             "DataOutputStream", "ObjectOutputStream", "FileChannel alt" };
  15.  
  16.     public static void main(String[] args) {
  17.         // create buffer
  18.         int[] ints = new int[NUM_INTS];
  19.         Random r = new Random();
  20.         for (int i = 0; i < NUM_INTS; i++)
  21.             ints[i] = r.nextInt();
  22.  
  23.         // run tests
  24.         double[][] results = new double[TESTS.length][NUM_TESTS];
  25.  
  26.         System.out.print("Running tests... ");
  27.         for (int i = 0; i < NUM_TESTS; i++) {
  28.             System.out.print(i + " ");
  29.  
  30.             results[0][i] = time("Buffered DataOutputStream", new IntWriter() {
  31.                 public void write(int[] ints) {
  32.                     storeBDO(ints);
  33.                 }
  34.             }, ints);
  35.  
  36.             results[1][i] = time("DataOutputStream", new IntWriter() {
  37.                 public void write(int[] ints) {
  38.                     storeDO(ints);
  39.                 }
  40.             }, ints);
  41.  
  42.             results[2][i] = time("ObjectOutputStream", new IntWriter() {
  43.                 public void write(int[] ints) {
  44.                     storeOO(ints);
  45.                 }
  46.             }, ints);
  47.  
  48.             results[3][i] = time("FileChannel alt", new IntWriter() {
  49.                 public void write(int[] ints) {
  50.                     storeFCAlt(ints);
  51.                 }
  52.             }, ints);
  53.         }
  54.         System.out.println();
  55.  
  56.         // print results
  57.         for (int i = 0; i < TESTS.length; i++) {
  58.             System.out.print(TESTS[i] + "\t");
  59.             for (int j = 0; j < NUM_TESTS; j++) {
  60.                 System.out.print(results[i][j] + "\t");
  61.             }
  62.             System.out.println();
  63.         }
  64.  
  65.         // time("FileChannel", new IntWriter() {
  66.         // public void write(int[] ints) {
  67.         // storeFC(ints);
  68.         // }
  69.         // }, ints);
  70.     }
  71.  
  72.     private static double time(String name, IntWriter writer, int[] ints) {
  73.         long start = System.nanoTime();
  74.         writer.write(ints);
  75.         long end = System.nanoTime();
  76.         double ms = (end - start) / 1000000d;
  77.         return ms;
  78.         // System.out.printf("%s wrote %,d ints in %,.3f ms%n", name,
  79.         // ints.length,
  80.         // ms);
  81.     }
  82.  
  83.     // ========================================================================
  84.     // tests
  85.     // ========================================================================
  86.  
  87.     private static void storeOO(int[] ints) {
  88.         ObjectOutputStream out = null;
  89.         try {
  90.             out = new ObjectOutputStream(new FileOutputStream("object.out"));
  91.             out.writeObject(ints);
  92.         } catch (IOException e) {
  93.             throw new RuntimeException(e);
  94.         } finally {
  95.             safeClose(out);
  96.         }
  97.     }
  98.  
  99.     private static void storeBDO(int[] ints) {
  100.         DataOutputStream out = null;
  101.         try {
  102.             out = new DataOutputStream(new BufferedOutputStream(
  103.                     new FileOutputStream("data.out")));
  104.             for (int anInt : ints) {
  105.                 out.write(anInt);
  106.             }
  107.         } catch (IOException e) {
  108.             throw new RuntimeException(e);
  109.         } finally {
  110.             safeClose(out);
  111.         }
  112.     }
  113.  
  114.     private static void storeDO(int[] ints) {
  115.         DataOutputStream out = null;
  116.         try {
  117.             out = new DataOutputStream(new FileOutputStream("data.out"));
  118.             for (int anInt : ints) {
  119.                 out.write(anInt);
  120.             }
  121.         } catch (IOException e) {
  122.             throw new RuntimeException(e);
  123.         } finally {
  124.             safeClose(out);
  125.         }
  126.     }
  127.  
  128.     private static void storeFC(int[] ints) {
  129.         FileOutputStream out = null;
  130.         try {
  131.             out = new FileOutputStream("fc.out");
  132.             FileChannel file = out.getChannel();
  133.             ByteBuffer buf = file.map(FileChannel.MapMode.READ_WRITE, 0,
  134.                     4 * ints.length);
  135.             for (int i : ints) {
  136.                 buf.putInt(i);
  137.             }
  138.             file.close();
  139.         } catch (IOException e) {
  140.             throw new RuntimeException(e);
  141.         } finally {
  142.             safeClose(out);
  143.         }
  144.     }
  145.  
  146.     private static void storeFCAlt(int[] ints) {
  147.         ByteBuffer buffer = ByteBuffer.allocate(4 * ints.length);
  148.         for (int i : ints)
  149.             buffer.putInt(i);
  150.  
  151.         FileChannel fc = null;
  152.         try {
  153.             fc = new FileOutputStream("fcalt.out").getChannel();
  154.             fc.write(buffer);
  155.         } catch (FileNotFoundException e) {
  156.             e.printStackTrace();
  157.         } catch (IOException e) {
  158.             e.printStackTrace();
  159.         } finally {
  160.             safeClose(fc);
  161.         }
  162.     }
  163.  
  164.     // safe close helpers
  165.  
  166.     private static void safeClose(OutputStream out) {
  167.         try {
  168.             if (out != null) {
  169.                 out.close();
  170.             }
  171.         } catch (IOException e) {
  172.             // do nothing
  173.         }
  174.     }
  175.  
  176.     private static void safeClose(FileChannel out) {
  177.         try {
  178.             if (out != null) {
  179.                 out.close();
  180.             }
  181.         } catch (IOException e) {
  182.             // do nothing
  183.         }
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement