Advertisement
zoltanvi

Java Thread speed comparison

Jun 30th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.  
  5.     public static volatile long start;
  6.     public static volatile long end;
  7.     public static void main(String[] args) {
  8.  
  9.         Random r = new Random();
  10.         int[] testArray1 = new int[100000];
  11.         int[] testArray2 = new int[100000];
  12.         int[] testArray3 = new int[100000];
  13.         int[] testArray4 = new int[100000];
  14.         for (int i = 0; i < testArray1.length; i++) {
  15.             testArray1[i] = r.nextInt(100000);
  16.             testArray2[i] = testArray1[i];
  17.             testArray3[i] = testArray1[i];
  18.             testArray4[i] = testArray1[i];
  19.         }
  20.  
  21.  
  22.         Thread t1 = new Thread(() -> {
  23.             InsertionSort(testArray1);
  24.             if(end == 0){
  25.                 end = 999;
  26.             } else {
  27.                 end = System.currentTimeMillis();
  28.                 System.out.println(end - start);
  29.             }
  30.  
  31.         });
  32.         Thread t2 = new Thread(() -> {
  33.             BubbleSort(testArray2);
  34.             if(end == 0){
  35.                 end = 999;
  36.             } else {
  37.                 end = System.currentTimeMillis();
  38.                 System.out.println(end - start);
  39.             }
  40.  
  41.         });
  42.  
  43.         start = System.currentTimeMillis();
  44.  
  45.         InsertionSort(testArray3);
  46.         BubbleSort(testArray4);
  47.  
  48.         end = System.currentTimeMillis();
  49.  
  50.  
  51.         System.out.println("Thread nΓ©lkΓΌl: ");
  52.         System.out.println(end - start);
  53.  
  54.         System.out.println("\nThreaddel:");
  55.         end = 0;
  56.         start = System.currentTimeMillis();
  57.  
  58.         t1.start();
  59.         t2.start();
  60.  
  61.  
  62.  
  63.  
  64.     }
  65.  
  66.     public static void InsertionSort(int[] data) {
  67.         for (int i = 0; i < data.length - 1; i++) {
  68.             for (int j = i + 1; j > 0; j--) {
  69.                 if (data[j - 1] < data[j]) {
  70.                     int temp = data[j - 1];
  71.                     data[j - 1] = data[j];
  72.                     data[j] = temp;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     public static void BubbleSort(int[] data) {
  79.         for (int i = 0; i < data.length; i++) {
  80.             for (int j = 1; j < (data.length - i); j++) {
  81.                 if (data[j - 1] < data[j]) {
  82.                     //swap elements
  83.                     int temp = data[j - 1];
  84.                     data[j - 1] = data[j];
  85.                     data[j] = temp;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement