1+ package bcGen .Arrays
2+
3+ import bcGen .Arrays .ArrayTestLib
4+
5+ object testArray {
6+ val size = 1000
7+ val maxInt = 10000
8+ val rng = new scala.util.Random (1234 )
9+ val intArr : Array [Int ] = Array .fill[Int ](size)(rng.nextInt(maxInt))
10+ val objArr : Array [Any ] = Array .fill[Any ](size)(new bcGen.Foo (rng.nextInt(maxInt)))
11+ val doubleArr : Array [Double ] = Array .fill[Double ](size)(rng.nextDouble())
12+ val intDoubleArr : Array [Any ] = Array .tabulate(size * 2 ) { i =>
13+ if (rng.nextInt % 2 == 0 ) rng.nextInt(maxInt) else rng.nextDouble()
14+ }
15+ val shortArrInt = Array .fill[Int ](10 )(rng.nextInt(maxInt))
16+ val shortArrDouble = Array .fill[Double ](10 )(rng.nextDouble())
17+ val shortArrObj = Array .fill[Any ](10 )(new bcGen.Foo (rng.nextInt(maxInt)))
18+ val shorArrIntCp = new Array [Int ](10 )
19+ val shorArrDoubleCp = new Array [Double ](10 )
20+ val shorArrObjCp = new Array [Any ](10 )
21+ val intArrCp = new Array [Int ](size)
22+ val anyArrCp = new Array [Any ](size)
23+ val doubleArrCp = new Array [Double ](size)
24+ val intDoubleArrCp = new Array [Any ](size * 2 )
25+
26+ var hashState : Long = 0
27+ val hashMod : Long = 998244353
28+ @ main def mainArr1 (): Unit =
29+ ArrayTestLib .copy[Int ](intArr, intArrCp)
30+ for (e <- intArrCp) {
31+ hashState = (hashState * 23 + e) % hashMod
32+ }
33+
34+ @ main def mainArr2 (): Unit =
35+ ArrayTestLib .copy[Any ](objArr, anyArrCp)
36+ for (e <- anyArrCp) {
37+ hashState = (hashState * 23 + e.asInstanceOf [bcGen.Foo ].id) % hashMod
38+ }
39+
40+ @ main def mainArr3 (): Unit =
41+ ArrayTestLib .copy[Double ](doubleArr, doubleArrCp)
42+ for (e <- doubleArrCp) {
43+ hashState = (hashState * 23 + java.lang.Double .doubleToRawLongBits(e) % hashMod) % hashMod
44+ }
45+
46+ @ main def mainArr4 (): Unit =
47+ ArrayTestLib .copy[Any ](intDoubleArr, intDoubleArrCp)
48+ for (e <- intDoubleArrCp) {
49+ e match {
50+ case i : Int => hashState = (hashState * 23 + i) % hashMod
51+ case d : Double => hashState = (hashState * 23 + java.lang.Double .doubleToRawLongBits(d) % hashMod) % hashMod
52+ case _ => {}
53+ }
54+ }
55+
56+ def genericCopy [T ](src : Array [T ], dst : Array [T ]): Unit = {
57+ ArrayTestLib .copy(src, dst)
58+ }
59+
60+ @ main def mainArr5 (): Unit =
61+ genericCopy[Int ](intArr, intArrCp)
62+ for (e <- intArrCp) {
63+ hashState = (hashState * 23 + e) % hashMod
64+ }
65+
66+ @ main def mainArr6 (): Unit =
67+ genericCopy[Any ](objArr, anyArrCp)
68+ for (e <- anyArrCp) {
69+ hashState = (hashState * 23 + e.asInstanceOf [bcGen.Foo ].id) % hashMod
70+ }
71+
72+ @ main def mainArr7 (): Unit =
73+ genericCopy[Double ](doubleArr, doubleArrCp)
74+ for (e <- doubleArrCp) {
75+ hashState = (hashState * 23 + java.lang.Double .doubleToRawLongBits(e) % hashMod) % hashMod
76+ }
77+
78+ @ main def mainArr8 (): Unit =
79+ genericCopy[Any ](intDoubleArr, intDoubleArrCp)
80+ for (e <- intDoubleArrCp) {
81+ e match {
82+ case i : Int => hashState = (hashState * 23 + i) % hashMod
83+ case d : Double => hashState = (hashState * 23 + java.lang.Double .doubleToRawLongBits(d) % hashMod) % hashMod
84+ case _ => {}
85+ }
86+ }
87+
88+ @ main def runChecksumInt (): Unit =
89+ val repeat = 10000
90+ var i = 1
91+ var sum : Long = 0
92+ while i <= repeat do
93+ sum += ArrayTestLib .checksum[Int ](intArr)
94+ i += 1
95+ println(sum)
96+
97+ @ main def runChecksumInt2 (): Unit =
98+ var i = 0
99+ while (i < shortArrInt.length) do {
100+ println(shortArrInt(i) + " : " + shortArrInt(i).## )
101+ i += 1
102+ }
103+ val sum = ArrayTestLib .checksum[Int ](shortArrInt)
104+ println(sum)
105+ val sum2 = ArrayTestLib .checksum[Int ](shortArrInt)
106+ println(sum2)
107+
108+ @ main def runAllArr (): Unit =
109+ val repeat = 1000
110+ var i = 1
111+ while i <= repeat do {
112+ mainArr1()
113+ i += 1
114+ }
115+ i = 1
116+ while i <= repeat do {
117+ mainArr2()
118+ i += 1
119+ }
120+ i = 1
121+ while i <= repeat do {
122+ mainArr3()
123+ i += 1
124+ }
125+ i = 1
126+ while i <= repeat do {
127+ mainArr4()
128+ i += 1
129+ }
130+ i = 1
131+ while i <= repeat do {
132+ mainArr5()
133+ i += 1
134+ }
135+ i = 1
136+ while i <= repeat do {
137+ mainArr6()
138+ i += 1
139+ }
140+ i = 1
141+ while i <= repeat do {
142+ mainArr7()
143+ i += 1
144+ }
145+ i = 1
146+ while i <= repeat do {
147+ mainArr8()
148+ i += 1
149+ }
150+ println(hashState)
151+
152+ @ main def runInt (): Unit =
153+ val repeat = 10000
154+ var i = 1
155+ while i <= repeat do
156+ ArrayTestLib .copy[Int ](intArr, intArrCp)
157+ i += 1
158+
159+ @ main def runMixed (): Unit =
160+ val repeat = 20000
161+ var i = 1
162+ var is = 0
163+ var anys = 0
164+ var ds = 0
165+ var elses = 0
166+ while i <= repeat do
167+ val n = rng.nextInt(4 )
168+ if (n == 0 ) {
169+ ArrayTestLib .copy[Int ](intArr, intArrCp)
170+ is += 1
171+ } else if (n == 1 ) {
172+ ArrayTestLib .copy[Any ](objArr, anyArrCp)
173+ anys += 1
174+ } else if (n == 2 ) {
175+ ArrayTestLib .copy[Double ](doubleArr, doubleArrCp)
176+ ds += 1
177+ } else {
178+ ArrayTestLib .copy[Any ](intDoubleArr, intDoubleArrCp)
179+ elses += 1
180+ }
181+ i += 1
182+ println(System .nanoTime())
183+ i = 1
184+ is = 0
185+ anys = 0
186+ ds = 0
187+ elses = 0
188+ while i <= repeat do
189+ val n = rng.nextInt(4 )
190+ if (n == 0 ) {
191+ ArrayTestLib .copy[Int ](intArr, intArrCp)
192+ is += 1
193+ } else if (n == 1 ) {
194+ ArrayTestLib .copy[Any ](objArr, anyArrCp)
195+ anys += 1
196+ } else if (n == 2 ) {
197+ ArrayTestLib .copy[Double ](doubleArr, doubleArrCp)
198+ ds += 1
199+ } else {
200+ ArrayTestLib .copy[Any ](intDoubleArr, intDoubleArrCp)
201+ elses += 1
202+ }
203+ i += 1
204+ println(is)
205+ println(anys)
206+ println(ds)
207+ println(elses)
208+ println(System .nanoTime())
209+ }
0 commit comments