Advertisement
Guest User

Integer vs MutableInteger vs Array

a guest
Sep 21st, 2012
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. /**
  2.  * Copyright (C) Luxoft.
  3.  * All rights reserved.
  4.  */
  5. package net.avalter.mutableInteger;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.Random;
  10.  
  11. /**
  12.  * @author avalter.
  13.  * @version 1.0
  14.  */
  15. public class SimpleInteger {
  16.   private static Random rnd = new Random();
  17.   public static final int SIZE = 1000000000;
  18.  
  19.  
  20.   public static void main(String[] args) throws InterruptedException {
  21.     Thread.sleep(10000);
  22.     for (int i = 0; i < 10; ++i) {
  23.       long start = System.nanoTime();
  24.       //Map<Byte, Integer> res = countStrings();
  25.       //Map<Byte, MutableInteger> res = countMutable();
  26.       Map<Byte, Integer> res = countArray();
  27.       long finish = System.nanoTime();
  28.       System.out.println(res.keySet());
  29.       System.out.println(finish - start);
  30.     }
  31.   }
  32.  
  33.   public static Map<Byte, Integer> countStrings() {
  34.     Map<Byte, Integer> counts = new HashMap<Byte, Integer>();
  35.     for (int i = 0; i < SIZE; ++i) {
  36.       byte next = getNextByte();
  37.       Integer val = counts.get(next);
  38.       if (val == null) {
  39.         counts.put(next, 1);
  40.       } else {
  41.         counts.put(next, val + 1);
  42.       }
  43.     }
  44.     return counts;
  45.   }
  46.  
  47.   public static Map<Byte, MutableInteger> countMutable() {
  48.     Map<Byte, MutableInteger> counts = new HashMap<Byte, MutableInteger>();
  49.     for (int i = 0; i < SIZE; ++i) {
  50.       byte next = getNextByte();
  51.       MutableInteger val = counts.get(next);
  52.       if(val == null) counts.put(next, new MutableInteger(1));
  53.       else val.increment();
  54.     }
  55.     return counts;
  56.   }
  57.  
  58.   public static Map<Byte, Integer> countArray() {
  59.     Map<Byte, Integer> pointer = new HashMap<Byte, Integer>();
  60.     int freeIndex = 0;
  61.     int[] data = new int[10];
  62.     for (int i = 0; i < SIZE; ++i) {
  63.       byte next = getNextByte();
  64.       Integer index = pointer.get(next);
  65.       if (index == null) {
  66.         pointer.put(next, freeIndex);
  67.         index = freeIndex;
  68.         freeIndex++;
  69.       }
  70.       data[index]++;
  71.     }
  72.     for (Byte b : pointer.keySet()) {
  73.       pointer.put(b, data[pointer.get(b)]);
  74.     }
  75.     return pointer;
  76.   }
  77.  
  78.   public static byte getNextByte() {
  79.     return (byte) rnd.nextInt(4);
  80.   }
  81.  
  82.   public static class MutableInteger {
  83.     private int value;
  84.  
  85.     public MutableInteger(int value) {
  86.       this.value = value;
  87.     }
  88.  
  89.     public int intValue() {
  90.       return value;
  91.     }
  92.  
  93.     public void set(int value) {
  94.       this.value = value;
  95.     }
  96.  
  97.     public void increment() {
  98.       value++;
  99.     }
  100.  
  101.     public String toString() {
  102.       return String.valueOf(value);
  103.     }
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement