Пример #1
1
 @Test
 @Override
 public void perform() {
   final long currentThreadId = Thread.currentThread().getId();
   String[] array = getWordsAsArray();
   // sort
   CopyOnWriteArraySet<Long> threadIds = new CopyOnWriteArraySet<>();
   Arrays.sort(
       array,
       (a, b) -> {
         threadIds.add(Thread.currentThread().getId());
         return a.compareTo(b);
       });
   assertEquals(1, threadIds.size());
   assertEquals(currentThreadId, threadIds.toArray()[0]);
   // parallelSort
   threadIds.clear();
   Arrays.parallelSort(
       array,
       (a, b) -> {
         threadIds.add(Thread.currentThread().getId());
         return a.compareTo(b);
       });
   assertTrue(threadIds.size() > 1);
 }
  // the naive way of doing things - interesting as a discussion starter
  public static void main(String[] args) {
    for (int size = 1; size < 1000; size++) {
      double[] array = DoubleStream.generate(Math::random).limit(size).toArray();
      double[] copiedArray = Arrays.copyOf(array, size);
      long parallelNanos =
          Timing.time(() -> Arrays.parallelSort(copiedArray)).getDuration().toNanos();
      long singleNanos = Timing.time(() -> Arrays.sort(array)).getDuration().toNanos();

      System.out.println("Size:" + size);
      System.out.println("Single threaded: " + singleNanos);
      System.out.println("Parallel: " + parallelNanos);
      System.out.println();
      if (singleNanos > parallelNanos) {
        return;
      }
    }
  }
 public static void sort(Algorithm algo, int[] a) {
   switch (algo) {
     case SSORT:
       ssort(a);
       break;
     case ISORT:
       isort(a);
       break;
     case ISORT_BIN:
       isortBin(a);
       break;
     case MSORT_BASIC:
       msortBasic(a);
       break;
     case MSORT_NO_GAR:
       msortNoGarbage(a);
       break;
     case MSORT_ALT:
       msortAlt(a);
       break;
     case PAR_MSORT:
       parallelMergesort(a);
       break;
     case QSORT_BASIC:
       qsortBasic(a);
       break;
     case QSORT_HOARE:
       qsortHoare(a);
       break;
     case PAR_QSORT:
       parallelQuicksort(a);
       break;
     case HSORT:
       hsort(a);
       break;
     case JAVASORT:
       Arrays.sort(a);
       break;
     case JAVASORT_PAR:
       Arrays.parallelSort(a);
       break;
   }
 }
Пример #4
1
  public ParallelTest() {
    StopWatch watch = new StopWatch("Timer");
    List<Integer> numbersToSum = new ArrayList<Integer>();

    System.out.println("Preparing data...");
    for (int i = 0; i < 1_000_000; i++) {
      numbersToSum.add(i);
    }
    System.out.println("Data prepared");
    watch.start("Parallel sum");
    Long sum = numbersToSum.stream().parallel().map(it -> it.longValue()).reduce(0L, Long::sum);
    watch.stop();

    watch.start("Sequence sum");
    sum = numbersToSum.stream().map(it -> it.longValue()).reduce(0L, Long::sum);
    watch.stop();

    Long[] arrayOfLong = new Long[20_000_000];
    // prepare array
    Arrays.parallelSetAll(arrayOfLong, it -> ThreadLocalRandom.current().nextLong(100_000));

    // make a copy
    Long[] arrayOfLongCopy = Arrays.copyOf(arrayOfLong, arrayOfLong.length);

    // print array
    Arrays.stream(arrayOfLong).limit(10).forEach(it -> System.out.print(it + ", "));
    System.out.println();
    watch.start("Parallel sort");
    // sort array and print
    Arrays.parallelSort(arrayOfLong);
    watch.stop();
    Arrays.stream(arrayOfLong).limit(10).forEach(it -> System.out.print(it + ", "));
    System.out.println();
    watch.start("Seq sort");
    Arrays.sort(arrayOfLongCopy);
    watch.stop();
    Arrays.stream(arrayOfLongCopy).limit(10).forEach(it -> System.out.print(it + ", "));

    System.out.println(watch.prettyPrint());
  }