Advertisement
Guest User

Untitled

a guest
May 30th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4.  
  5. public class Bench {
  6.  
  7.     public static void main(String[] args){
  8.  
  9.         // First solution
  10.         Random r = new Random();
  11.         List<String> initialList = new ArrayList<String>(21);
  12.         for (int i=0;i<160;i++) {
  13.             initialList.add("test");
  14.         }
  15.         for (int i=0; i<40; i++) {
  16.             initialList.add(r.nextInt(initialList.size()-1), "test2");
  17.         }
  18.        
  19.         // Test
  20.         List<String> newList1 = new ArrayList<String>(initialList);
  21.         for (String item : initialList) {
  22.             if (item.length() > 4) {
  23.                 newList1.remove(item);
  24.             }
  25.         }
  26.         assert(newList1.size() == 160);
  27.  
  28.         long startTime = System.currentTimeMillis();
  29.  
  30.         for (int i=0;i<1000000;i++) {
  31.             List<String> newList = new ArrayList<String>(initialList);
  32.             for (String item : initialList) {
  33.                 if (item.length() > 4) {
  34.                     newList.remove(item);
  35.                 }
  36.             }
  37.         }
  38.  
  39.         long endTime = System.currentTimeMillis();
  40.         System.out.println("First : "
  41.                         + (endTime - startTime)+ " ms");
  42.        
  43.        
  44.        
  45.         // Second solution
  46.        
  47.         // Test
  48.         List<String> newList2 = new ArrayList<String>();
  49.         for (String item : initialList) {
  50.             if (item.length() <= 4) {
  51.                 newList2.add(item);
  52.             }
  53.         }
  54.         assert(newList2.size() == 16);
  55.  
  56.         long startTime1 = System.currentTimeMillis();
  57.  
  58.         for (int i=0;i<1000000;i++) {
  59.             List<String> newList = new ArrayList<String>();
  60.             for (String item : initialList) {
  61.                 if (item.length() <= 4) {
  62.                     newList.add(item);
  63.                 }
  64.             }
  65.         }
  66.  
  67.         long endTime1 = System.currentTimeMillis();
  68.         System.out.println("Second :  "
  69.                         + (endTime1 - startTime1)+ " ms");
  70.        
  71.        
  72.        
  73.         // Third solution
  74.        
  75.         // Test
  76.         List<String> newList3 = new ArrayList<String>(initialList.size()+1);
  77.         for (String item : initialList) {
  78.             if (item.length() <= 4) {
  79.                 newList3.add(item);
  80.             }
  81.         }
  82.         assert(newList3.size() == 160);
  83.  
  84.         long startTime2 = System.currentTimeMillis();
  85.  
  86.         for (int i=0;i<1000000;i++) {
  87.             List<String> newList = new ArrayList<String>(initialList.size()+1);
  88.             for (String item : initialList) {
  89.                 if (item.length() <= 4) {
  90.                     newList.add(item);
  91.                 }
  92.             }
  93.         }
  94.  
  95.         long endTime2 = System.currentTimeMillis();
  96.         System.out.println("Third :  "
  97.                         + (endTime2 - startTime2)+ " ms");
  98.  
  99.        
  100.        
  101.        
  102.         // Fourth solution
  103.        
  104.         // Test
  105.         List<String> newList4 = new ArrayList<String>(initialList.size());
  106.         for (String item : initialList) {
  107.             if (item.length() <= 4) {
  108.                 newList4.add(item);
  109.             }
  110.         }
  111.         assert(newList4.size() == 160);
  112.  
  113.         long startTime4 = System.currentTimeMillis();
  114.  
  115.         for (int i=0;i<1000000;i++) {
  116.             List<String> newList = new ArrayList<String>(initialList.size());
  117.             for (String item : initialList) {
  118.                 if (item.length() <= 4) {
  119.                     newList.add(item);
  120.                 }
  121.             }
  122.         }
  123.  
  124.         long endTime4 = System.currentTimeMillis();
  125.         System.out.println("Fourth :  "
  126.                         + (endTime4 - startTime4)+ " ms");
  127.  
  128.         // Fifth solution
  129.        
  130.         // Test
  131.         {
  132.             String[] arr = new String[initialList.size()];
  133.             String[] src = initialList.toArray(new String[initialList.size()]);
  134.             int dstIndex = 0, blockStartIdx=0, blockSize=0;
  135.             for (int currIdx=0; currIdx < initialList.size(); currIdx++) {
  136.                 String item = src[currIdx];
  137.                 if (item.length() <= 4) {
  138.                     if (blockSize > 0)
  139.                         System.arraycopy(src, blockStartIdx, arr, dstIndex, blockSize);
  140.                     dstIndex += blockSize;
  141.                     blockSize = 0;
  142.                 } else {
  143.                     if (blockSize == 0)
  144.                         blockStartIdx = currIdx;
  145.                     blockSize++;
  146.                 }
  147.             }
  148.             assert(arr.length == 160);
  149.         }
  150.        
  151.         long startTime5 = System.currentTimeMillis();
  152.  
  153.         for (int i=0;i<1000000;i++) {
  154.             String[] arr = new String[initialList.size()];
  155.             String[] src = initialList.toArray(new String[initialList.size()]);
  156.             int dstIndex = 0, blockStartIdx=0, blockSize=0;
  157.             for (int currIdx=0; currIdx < initialList.size(); currIdx++) {
  158.                 String item = src[currIdx];
  159.                 if (item.length() <= 4) {
  160.                     if (blockSize > 0)
  161.                         System.arraycopy(src, blockStartIdx, arr, dstIndex, blockSize);
  162.                     dstIndex += blockSize;
  163.                     blockSize = 0;
  164.                 } else {
  165.                     if (blockSize == 0)
  166.                         blockStartIdx = currIdx;
  167.                     blockSize++;
  168.                 }
  169.             }
  170.             assert(arr.length == 160);
  171.         }
  172.  
  173.         long endTime5 = System.currentTimeMillis();
  174.         System.out.println("Fifth :  "
  175.                         + (endTime5 - startTime5)+ " ms");
  176.  
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement