File tree 1 file changed +57
-0
lines changed
core/src/main/resources/examples
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class DoesItVectoriseValue
2
+ {
3
+ public DoesItVectoriseValue ()
4
+ {
5
+ int [] array = new int [1024 ];
6
+
7
+ for (int i = 0 ; i < 1_000_000 ; i ++)
8
+ {
9
+ incrementArray (array , 1 );
10
+ }
11
+
12
+ for (int i = 0 ; i < array .length ; i ++)
13
+ {
14
+ System .out .println (array [i ]);
15
+ }
16
+ }
17
+
18
+ public void incrementArray (int [] array , int constant )
19
+ {
20
+ int length = array .length ;
21
+
22
+ for (Cursor c = Cursor .of (length ); c .canAdvance (); c = c .advance ())
23
+ {
24
+ array [c .position ] += constant ;
25
+ }
26
+ }
27
+
28
+ public value record Cursor (int position , int length )
29
+ {
30
+ public Cursor {
31
+ if (length < 0 || position > length )
32
+ {
33
+ throw new IllegalArgumentException ();
34
+ }
35
+ }
36
+
37
+ public static Cursor of (int length )
38
+ {
39
+ return new Cursor (0 , length );
40
+ }
41
+
42
+ public boolean canAdvance ()
43
+ {
44
+ return position < length ;
45
+ }
46
+
47
+ public Cursor advance ()
48
+ {
49
+ return new Cursor (position + 1 , length );
50
+ }
51
+ }
52
+
53
+ public static void main (String [] args )
54
+ {
55
+ new DoesItVectoriseValue ();
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments