Skip to content

Commit 575c194

Browse files
committed
feat: add Java 26 Vector API demo with explanation
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent 8819c32 commit 575c194

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import jdk.incubator.vector.IntVector;
2+
import jdk.incubator.vector.VectorSpecies;
3+
4+
public class VectorApiDemo {
5+
6+
static final VectorSpecies<Integer> SPECIES = IntVector.SPECIES_PREFERRED;
7+
8+
public static void main(String[] args) {
9+
10+
int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
11+
int[] b = {10, 20, 30, 40, 50, 60, 70, 80};
12+
int[] result = new int[a.length];
13+
14+
addVectors(a, b, result);
15+
16+
System.out.println("Vector Addition Result:");
17+
for (int value : result) {
18+
System.out.print(value + " ");
19+
}
20+
}
21+
22+
static void addVectors(int[] a, int[] b, int[] result) {
23+
24+
int i = 0;
25+
int upperBound = SPECIES.loopBound(a.length);
26+
27+
// Vectorized part
28+
for (; i < upperBound; i += SPECIES.length()) {
29+
var va = IntVector.fromArray(SPECIES, a, i);
30+
var vb = IntVector.fromArray(SPECIES, b, i);
31+
var vc = va.add(vb);
32+
vc.intoArray(result, i);
33+
}
34+
35+
// Remaining elements (scalar fallback)
36+
for (; i < a.length; i++) {
37+
result[i] = a[i] + b[i];
38+
}
39+
}
40+
}
41+
42+
/*
43+
What changed: Previous vs New
44+
45+
Previous Java style:
46+
- Used normal for-loop for array calculations
47+
- One element processed at a time
48+
- Limited CPU optimization
49+
50+
Example:
51+
for (int i = 0; i < a.length; i++) {
52+
result[i] = a[i] + b[i];
53+
}
54+
55+
New Java 26 style:
56+
- Uses Vector API
57+
- Multiple elements processed together
58+
- Better use of SIMD instructions on supported CPUs
59+
60+
Why the new approach is better:
61+
- Faster for large numeric workloads
62+
- Better CPU utilization
63+
- Useful in performance-heavy applications
64+
- Cleaner API for vectorized operations
65+
66+
Pros:
67+
1. Better performance
68+
- Multiple values processed in one operation
69+
70+
2. Modern computation style
71+
- Uses SIMD where hardware supports it
72+
73+
3. Scalable
74+
- Good for math-heavy programs
75+
76+
4. Built-in API
77+
- No external library needed
78+
79+
Cons:
80+
1. Incubator feature
81+
- Vector API is still incubating in Java 26
82+
83+
2. More complex than simple loops
84+
- Harder for beginners compared to basic for-loop
85+
86+
3. Best for specific use cases
87+
- Useful mainly in data-heavy calculations
88+
89+
4. Requires module flag
90+
- Must compile and run with incubator module enabled
91+
92+
Best use case:
93+
- Image processing
94+
- Scientific calculations
95+
- Machine learning preprocessing
96+
- Large array computations
97+
98+
Compile and run:
99+
javac --add-modules jdk.incubator.vector VectorApiDemo.java
100+
java --add-modules jdk.incubator.vector VectorApiDemo
101+
*/

0 commit comments

Comments
 (0)