Advertisement
KIKIJIKI

Benchmark

Oct 8th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.charset.StandardCharsets;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5.  
  6.  
  7. public class Test {
  8.     private static class PathReader {
  9.         private String inputString;
  10.         private int currentPosition = 0;
  11.         private int length = 0;
  12.  
  13.         public PathReader(String string) {
  14.             inputString = string;
  15.             length = inputString.length();
  16.  
  17.             currentPosition = 0;
  18.         }
  19.  
  20.         public boolean hasNext() {
  21.             skipSpaces();
  22.             return currentPosition < length;
  23.         }
  24.  
  25.         public char getNext() {
  26.             return inputString.charAt(currentPosition++);
  27.         }
  28.  
  29.         public char peekNext() {
  30.             return inputString.charAt(currentPosition);
  31.         }
  32.  
  33.         private void skipCharacter() {
  34.             currentPosition++;
  35.         }
  36.  
  37.         private void skipSpaces() {
  38.             while (currentPosition < length) {
  39.                 char c = peekNext();
  40.  
  41.                 switch (c) {
  42.                     case ' ':
  43.                     case ',':
  44.                     case '\n':
  45.                     case '\t':
  46.                     case '\r':
  47.                         break;
  48.  
  49.                     default:
  50.                         return;
  51.                 }
  52.  
  53.                 skipCharacter();
  54.             }
  55.         }
  56.  
  57.         public float parseFloat() {
  58.             boolean exp = false;
  59.  
  60.             int j = currentPosition;
  61.  
  62.             if (inputString.charAt(j) == '-') {
  63.                 getNext();
  64.             }
  65.  
  66.             while (currentPosition < length) {
  67.                 char c = getNext();
  68.  
  69.                 switch (c) {
  70.                     case '0':
  71.                     case '1':
  72.                     case '2':
  73.                     case '3':
  74.                     case '4':
  75.                     case '5':
  76.                     case '6':
  77.                     case '7':
  78.                     case '8':
  79.                     case '9':
  80.                     case '+':
  81.                     case '.':
  82.                         exp = false;
  83.                         break;
  84.  
  85.                     case '-':
  86.                         if (exp) {
  87.                             exp = false;
  88.                         } else {
  89.                             currentPosition--;
  90.                             return Float.parseFloat(inputString.substring(j, currentPosition));
  91.                         }
  92.                         break;
  93.  
  94.                     case 'e':
  95.                     case 'E':
  96.                         exp = true;
  97.                         break;
  98.  
  99.                     default:
  100.                         currentPosition--;
  101.                         return Float.parseFloat(inputString.substring(j, currentPosition));
  102.                 }
  103.             }
  104.  
  105.             return Float.parseFloat(inputString.substring(j, currentPosition));
  106.         }
  107.     }
  108.  
  109.     public static void main(String[] args) throws IOException {
  110.         String data = new String(Files.readAllBytes(Paths.get("test.txt")), StandardCharsets.UTF_8);
  111.  
  112.         // TEST 1
  113.         long time = System.nanoTime();
  114.        
  115.         for( int i = 1; i < 1000; i++ ){
  116.             PathReader reader = new PathReader(data);
  117.             while(reader.hasNext()){ reader.parseFloat(); }
  118.         }
  119.        
  120.         time = System.nanoTime() - time;
  121.        
  122.         System.out.println("Using reader: " + time);
  123.        
  124.         // TEST 2
  125.         time = System.nanoTime();
  126.        
  127.         for( int i = 1; i < 1000; i++ ){
  128.             data.replaceAll("[^\\d.-]|(?=-)", " ").trim().split(" +");
  129.         }
  130.        
  131.         time = System.nanoTime() - time;
  132.        
  133.         System.out.println("Using regexp: " + time);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement