Last active
September 13, 2022 12:08
-
-
Save denismakogon/270b595cb7553332ae180a52d2d8f691 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jdk.incubator.vector.DoubleVector; | |
import jdk.incubator.vector.VectorSpecies; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.util.Random; | |
import java.util.function.Supplier; | |
import java.util.stream.IntStream; | |
public class Task { | |
private static final VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_PREFERRED; | |
static double[] sumOf_vectorized(double[] a, double[] b) { | |
var operandSize = a.length; | |
var result = new double[operandSize]; | |
var upperBound = SPECIES.loopBound(operandSize); | |
IntStream.iterate(0, i -> i < upperBound, i -> i + SPECIES.length()).forEachOrdered(i -> { | |
var va = DoubleVector.fromArray(SPECIES, a, i); | |
var vb = DoubleVector.fromArray(SPECIES, b, i); | |
var vc = va.add(vb); | |
vc.intoArray(result, i); | |
}); | |
for (int i = upperBound; i < a.length; i++) { | |
result[i] = a[i] + b[i]; | |
} | |
return result; | |
} | |
static double[] sumOf_forLoop(double[] a, double[] b) { | |
var operandSize = a.length; | |
var result = new double[operandSize]; | |
for(int i = 0; i < operandSize; i++) { | |
result[i] = a[i] + b[i]; | |
} | |
return result; | |
} | |
static double[] sumOf_streamed(double[] a, double[] b) { | |
return IntStream.range(0, a.length).mapToDouble(index -> a[index] + b[index]).toArray(); | |
} | |
public static void main(String... args) { | |
var vectorSize = 1_000_000; | |
var r = new Random(); | |
Supplier<double[]> supplier = () -> IntStream.range(0, vectorSize) | |
.mapToDouble(i -> i * r.nextDouble()).toArray(); | |
var a = supplier.get(); | |
var b = supplier.get(); | |
var tries = 300_00; | |
var vectorizedAverage = IntStream.range(0, tries).mapToLong(index -> { | |
var now = Instant.now(); | |
sumOf_vectorized(a, b); | |
var later = Instant.now(); | |
return Duration.between(now, later).toMillis(); | |
}).average().getAsDouble(); | |
System.out.printf("Average vectorized sum took %f milliseconds.\n", vectorizedAverage); | |
var streamedAverage = IntStream.range(0, tries).mapToLong(index -> { | |
var now = Instant.now(); | |
sumOf_streamed(a, b); | |
var later = Instant.now(); | |
return Duration.between(now, later).toMillis(); | |
}).average().getAsDouble(); | |
System.out.printf("Average stream-bound sum took %f milliseconds.\n", streamedAverage); | |
var forLoopAverage = IntStream.range(0, tries).mapToLong(index -> { | |
var now = Instant.now(); | |
sumOf_forLoop(a, b); | |
var later = Instant.now(); | |
return Duration.between(now, later).toMillis(); | |
}).average().getAsDouble(); | |
System.out.printf("Average for-loop-bound sum took %f milliseconds.\n", forLoopAverage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment