Guest User

Comparision of algorithms (Java)

a guest
Jul 30th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. package org.acme.logfilter;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4.  
  5. /**
  6.  * Comparison of implementations removing multiple zero bytes (four or more)
  7.  * from an input array.
  8.  *
  9.  * removeDuplicates1() was proposed by another answer in this question:
  10.  *
  11.  * Related to https://stackoverflow.com/questions/1387027/j
  12.  */
  13. public class StackoverflowTestcase {
  14.    
  15.     // Implementation from https://stackoverflow.com/a/1387184/1078886
  16.     private static byte[] removeDuplicates1(byte[] inBytes) throws UnsupportedEncodingException {
  17.         String s0 = new String(inBytes, "ISO-8859-1");
  18.         String s1 = s0.replaceAll("\\x00{4,}", "");
  19.         return s1.getBytes("ISO-8859-1");
  20.     }
  21.    
  22.  
  23.     /**
  24.      * Remove four or more zero byte sequences from the input array.
  25.      *  
  26.      * @param inBytes the input array
  27.      * @return a new array with four or more zero bytes removed form the input array
  28.      */
  29.     private static byte[] removeDuplicates2(byte[] inBytes) {
  30.         int size = inBytes.length;
  31.         // Use an array with the same size in the first place
  32.         byte[] newBytes = new byte[size];
  33.         byte value;
  34.         int newIdx = 0;
  35.         int zeroCounter = 0;
  36.        
  37.         for (int i = 0; i < size; i++) {
  38.             value = inBytes[i];
  39.            
  40.             if (value == 0) {
  41.                 zeroCounter++;
  42.             } else {
  43.                 if (zeroCounter >= 4) {
  44.                     // Rewind output buffer index
  45.                     newIdx -= zeroCounter;
  46.                 }
  47.                
  48.                 zeroCounter = 0;
  49.             }
  50.                        
  51.             newBytes[newIdx] = value;
  52.             newIdx++;
  53.         }
  54.        
  55.         if (zeroCounter >= 4) {
  56.             // Rewind output buffer index for four zero bytes at the end too
  57.             newIdx -= zeroCounter;
  58.         }
  59.        
  60.         // Copy data into an array that has the correct length
  61.         byte[] finalOut = new byte[newIdx];
  62.         System.arraycopy(newBytes, 0, finalOut, 0, newIdx);
  63.        
  64.         return finalOut;
  65.     }
  66.    
  67.     public static void test1(byte[] inBytes) throws UnsupportedEncodingException {
  68.         for (int i = 0; i < ITERATIONS; i++) {
  69.             removeDuplicates1(inBytes);
  70.         }
  71.     }
  72.    
  73.     public static void test2(byte[] inBytes) {
  74.         for (int i = 0; i < ITERATIONS; i++) {
  75.             removeDuplicates2(inBytes);
  76.         }
  77.     }
  78.  
  79.     // Test multiple times to average out fluctuations
  80.     private static final int ITERATIONS = 10000;
  81.  
  82.     public static void main(String[] args) throws UnsupportedEncodingException {
  83.         byte[] bytes;
  84.        
  85.         // Prepare the test data with some zero byte patterns added
  86.  
  87.         // Buffer size
  88.         int size = 32768 * 8;
  89.         // Enable bad case: 0101... vs. 1111111...000
  90.         boolean alternate = true;
  91.         // Add four or more zeros to the array (actually necessary for this test case)
  92.         boolean addZeroSequences = true;
  93.         // Increase the zero sequence length by one with each addition (add variation)
  94.         boolean increaseZeroSequences = false;
  95.         // Controls how frequent zero sequences are spread
  96.         // (The lower the more often zero sequences appear)
  97.         int zeroSequenceCount = 8192; // 32768;
  98.         // Increased with each occurrence
  99.         int zeroSequenceLen = 4;
  100.        
  101.         bytes = new byte[size];
  102.         int zeroSeqCount;
  103.        
  104.         for (int i = 0; i < size; i++) {
  105.             if (addZeroSequences && (i % zeroSequenceCount) == 0) {
  106.                 zeroSeqCount = zeroSequenceLen;
  107.                 // Increase length with every sequence
  108.                 if (increaseZeroSequences) {
  109.                     zeroSequenceLen++;
  110.                 }
  111.                
  112.                 while (zeroSeqCount > 0 && i < size) {
  113.                     bytes[i] = 0;
  114.                     zeroSeqCount--;
  115.                     i++;
  116.                 }
  117.             } else {
  118.                 if (alternate) {
  119.                     bytes[i] = ((i % 2 == 0) ? (byte) 1 : (byte) 0);
  120.                 } else {
  121.                     bytes[i] = 1;
  122.                 }
  123.             }
  124.         }
  125.  
  126.        
  127.         long t0;
  128.         float d1, d2, d3;
  129.        
  130.         t0 = System.currentTimeMillis();
  131.         test1(bytes);
  132.         d1 = (System.currentTimeMillis() - t0);
  133.         System.out.println(d1 + "ms");
  134.        
  135.         t0 = System.currentTimeMillis();
  136.         test2(bytes);
  137.         d2 = (System.currentTimeMillis() - t0);
  138.         System.out.println(d2 + "ms");
  139.        
  140.         System.out.println(String.format("Algorithm #1 is %.02fx slower than #2", (d1 / d2)));
  141.     }
  142. }
Add Comment
Please, Sign In to add comment