Guest User

Untitled

a guest
Jul 27th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.Random;
  9.  
  10. import static java.lang.System.currentTimeMillis;
  11.  
  12. public class Test {
  13.     public static void main(String[] args) throws IOException {
  14.         Random random = new Random();
  15.         for (int attempt = 0; attempt < 10; attempt++) {
  16.             byte[] array = new byte[2_000_000_000];
  17.             for (int i = 0; i < array.length; i++) {
  18.                 if (random.nextInt(64) == 0) {
  19.                     array[i] = 13;
  20.                 } else {
  21.                     array[i] = (byte) (32 + random.nextInt(127 - 32));
  22.                 }
  23.             }
  24.             int checksum = 0;
  25.             long startTime = currentTimeMillis();
  26.             for (int i = 0; i < array.length; i++) {
  27.                 checksum ^= array[i];
  28.             }
  29.             System.out.println(currentTimeMillis() - startTime);
  30.             startTime = currentTimeMillis();
  31.             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(array);
  32.             InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream, StandardCharsets.UTF_8);
  33.             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  34.             String line;
  35.             while ((line = bufferedReader.readLine()) != null) {
  36.                 var lineLength = line.length();
  37.                 for (int i = 0; i < lineLength; i++) {
  38.                     checksum ^= line.charAt(i);
  39.                 }
  40.                 checksum ^= 13;
  41.             }
  42.             System.out.println(currentTimeMillis() - startTime);
  43.             System.out.println(checksum);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment