SHARE
TWEET

Untitled

a guest Mar 16th, 2016 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BitPerfTest {
  2.         static final int SIZE = 50000000;
  3.         static int[] a = new int[SIZE];
  4.         static int[] b = new int[SIZE];
  5.        
  6.         static int[] c = new int[SIZE];
  7.         static java.util.Random rand = new java.util.Random();
  8.        
  9.     public static void main(String... args) {
  10.        
  11.         fill(a);
  12.         fillSmall(b);
  13.        
  14.         run();
  15.         run();
  16.         run();
  17.         run();
  18.         run();
  19.     }
  20.    
  21.     public static void run() {
  22.         add();
  23.         mult();
  24.         mod();
  25.         and();
  26.         or();
  27.         shiftLeft();
  28.         shiftRight();
  29.     }
  30.  
  31.     static void fill(int[] x) {
  32.         long then = System.currentTimeMillis();
  33.         for(int i = 0; i < SIZE; i++) {
  34.             x[i] = rand.nextInt(2000000000) + 1;
  35.         }
  36.         long now = System.currentTimeMillis();
  37.         System.out.println("fill in " + (now - then) + " millis");
  38.     }
  39.    
  40.     static void fillSmall(int[] x) {
  41.         long then = System.currentTimeMillis();
  42.         for(int i = 0; i < SIZE; i++) {
  43.             x[i] = rand.nextInt(31) + 1;
  44.         }
  45.         long now = System.currentTimeMillis();
  46.         System.out.println("fillSmall in " + (now - then) + " millis");
  47.     }
  48.    
  49.     static void add() {
  50.         long then = System.currentTimeMillis();
  51.         for(int i = 0; i < SIZE; i++) {
  52.             c[i] = a[i] + b[i];
  53.         }
  54.         long now = System.currentTimeMillis();
  55.         System.out.println("add in " + (now - then) + " millis");
  56.     }
  57.    
  58.     static void mult() {
  59.         long then = System.currentTimeMillis();
  60.         for(int i = 0; i < SIZE; i++) {
  61.             c[i] = a[i] * b[i];
  62.         }
  63.         long now = System.currentTimeMillis();
  64.         System.out.println("mult in " + (now - then) + " millis");
  65.     }
  66.    
  67.     static void mod() {
  68.         long then = System.currentTimeMillis();
  69.         for(int i = 0; i < SIZE; i++) {
  70.             c[i] = a[i] % b[i];
  71.         }
  72.         long now = System.currentTimeMillis();
  73.         System.out.println("mod in " + (now - then) + " millis");
  74.     }
  75.    
  76.     static void and() {
  77.         long then = System.currentTimeMillis();
  78.         for(int i = 0; i < SIZE; i++) {
  79.             c[i] = a[i] & b[i];
  80.         }
  81.         long now = System.currentTimeMillis();
  82.         System.out.println("and in " + (now - then) + " millis");
  83.     }
  84.    
  85.     static void or() {
  86.         long then = System.currentTimeMillis();
  87.         for(int i = 0; i < SIZE; i++) {
  88.             c[i] = a[i] | b[i];
  89.         }
  90.         long now = System.currentTimeMillis();
  91.         System.out.println("or in " + (now - then) + " millis");
  92.     }
  93.    
  94.     static void shiftLeft() {
  95.         long then = System.currentTimeMillis();
  96.         for(int i = 0; i < SIZE; i++) {
  97.             c[i] = a[i] << b[i];
  98.         }
  99.         long now = System.currentTimeMillis();
  100.         System.out.println("shiftLeft in " + (now - then) + " millis");
  101.     }
  102.    
  103.     static void shiftRight() {
  104.         long then = System.currentTimeMillis();
  105.         for(int i = 0; i < SIZE; i++) {
  106.             c[i] = a[i] >> b[i];
  107.         }
  108.         long now = System.currentTimeMillis();
  109.         System.out.println("shiftRight in " + (now - then) + " millis");
  110.     }
  111.  
  112.  
  113. }
  114.  
  115.  
  116. C:\files\j>javac BitPerfTest.java
  117.  
  118. C:\files\j>java BitPerfTest
  119. fill in 666 millis
  120. fillSmall in 623 millis
  121. add in 54 millis
  122. mult in 56 millis
  123. mod in 158 millis
  124. and in 57 millis
  125. or in 54 millis
  126. shiftLeft in 51 millis
  127. shiftRight in 54 millis
  128. add in 55 millis
  129. mult in 57 millis
  130. mod in 177 millis
  131. and in 54 millis
  132. or in 49 millis
  133. shiftLeft in 53 millis
  134. shiftRight in 52 millis
  135. add in 49 millis
  136. mult in 49 millis
  137. mod in 150 millis
  138. and in 50 millis
  139. or in 56 millis
  140. shiftLeft in 46 millis
  141. shiftRight in 46 millis
  142. add in 49 millis
  143. mult in 50 millis
  144. mod in 147 millis
  145. and in 49 millis
  146. or in 49 millis
  147. shiftLeft in 45 millis
  148. shiftRight in 46 millis
  149. add in 49 millis
  150. mult in 51 millis
  151. mod in 158 millis
  152. and in 49 millis
  153. or in 49 millis
  154. shiftLeft in 48 millis
  155. shiftRight in 46 millis
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top