Advertisement
cat_baxter

Programming Question-4: Fast file reading using java

Apr 18th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.RandomAccessFile;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.FileChannel;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. public class test {
  11.  
  12.     private static int[] a;
  13.  
  14.     static private int readInt(ByteBuffer buffer) throws IOException {
  15.         int res = 0;
  16.         boolean started = false;
  17.         for (int pos = buffer.position(); pos < buffer.limit(); pos++) {
  18.             int v = buffer.get() - '0';
  19.             if (v >= 0 && v < 10) {
  20.                 if (!started) started = true;
  21.                 res = res * 10 + v;
  22.             } else if (started)
  23.                 break;
  24.         }
  25.         return res;
  26.     }
  27.  
  28.     public void process(String filename) throws IOException{
  29.         long time = -System.nanoTime();
  30.         int n = 5105042;
  31.         a = new int[n];
  32.         RandomAccessFile ra = new RandomAccessFile(filename, "r");
  33.         long size = ra.length();
  34.         FileChannel channel = ra.getChannel();
  35.         ByteBuffer buffer = ByteBuffer.allocate((int) size);
  36.         channel.read(buffer);
  37.         buffer.position(0);
  38.         Map<Integer,List<Integer>> g = new HashMap<Integer, List<Integer>>();
  39.         for(int i = 0; i < n; i++) a[i] = readInt(buffer);
  40.         for(int i = 0; i < n/2; i++){
  41.             int src = a[i/2];
  42.             int dst = a[i/2+1];
  43.             List<Integer> edges = g.get(src);
  44.             if (edges == null){
  45.                 edges = new ArrayList<Integer>();
  46.                 g.put(src, edges);
  47.             }
  48.             edges.add(dst);
  49.         }
  50.         long rtime = System.nanoTime()+time;
  51.         time  += System.nanoTime();
  52.         System.out.println("Reading time (ms)   : " + rtime/1E6);
  53.         System.out.println("Total time (ms)     : " + time/1E6);
  54.     }
  55.  
  56.     static public void main(String[] args) throws IOException {
  57.         new test().process(args[0]);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement