1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| public class Main {
public static void main(String[] args) {
int rep = 100, size = 10000000;
int[] res = {0, 0, 0};
long tinit, t1, t2, t3, tter;
List<String> lstr = new ArrayList<>();
for (int i = 0; i < 10000000; i++) {
lstr.add(String.valueOf(i % 10));
}
String[] strings = lstr.toArray(new String[0]);
for (int i = 0; i < rep; i++) {
int tmp1, tmp2, tmp3;
tinit = System.currentTimeMillis();
int[] nums1 = Arrays.stream(strings).mapToInt(Integer::parseInt).toArray();
t1 = System.currentTimeMillis();
int[] nums2 = Arrays.asList(strings).stream().mapToInt(Integer::parseInt).toArray();
t2 = System.currentTimeMillis();
int[] nums3 = new int[strings.length];
for (int j = 0; j < strings.length; j++) {
nums3[j] = Integer.parseInt(strings[j]);
}
tter = System.currentTimeMillis();
tmp1 = (int)(t1 - tinit);
tmp2 = (int)(t2 - t1);
tmp3 = (int)(tter - t2);
System.out.println("rep : " + i + " \tnums1 = " + tmp1 + "\tnum2 = " + tmp2 + "\tnum3 = " + tmp3);
res[0] += tmp1;
res[1] += tmp2;
res[2] += tmp3;
}
System.out.println("Size : " + size + " Iteration : " + rep);
System.out.println("num1 : " + (res[0] / rep) + "ms");
System.out.println("num2 : " + (res[1] / rep) + "ms");
System.out.println("num3 : " + (res[2] / rep) + "ms");
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| rep : 0 nums1 = 100 num2 = 70 num3 = 43
rep : 1 nums1 = 52 num2 = 44 num3 = 64
rep : 2 nums1 = 43 num2 = 55 num3 = 47
rep : 3 nums1 = 43 num2 = 43 num3 = 41
rep : 4 nums1 = 42 num2 = 42 num3 = 40
rep : 5 nums1 = 43 num2 = 41 num3 = 40
rep : 6 nums1 = 42 num2 = 41 num3 = 41
rep : 7 nums1 = 42 num2 = 40 num3 = 40
rep : 8 nums1 = 41 num2 = 41 num3 = 39
rep : 9 nums1 = 41 num2 = 41 num3 = 40
...
rep : 90 nums1 = 41 num2 = 41 num3 = 41
rep : 91 nums1 = 42 num2 = 43 num3 = 41
rep : 92 nums1 = 42 num2 = 40 num3 = 40
rep : 93 nums1 = 41 num2 = 41 num3 = 41
rep : 94 nums1 = 42 num2 = 41 num3 = 40
rep : 95 nums1 = 42 num2 = 42 num3 = 42
rep : 96 nums1 = 41 num2 = 40 num3 = 41
rep : 97 nums1 = 42 num2 = 41 num3 = 40
rep : 98 nums1 = 44 num2 = 40 num3 = 40
rep : 99 nums1 = 41 num2 = 41 num3 = 39
Size : 10000000 Iteration : 100
num1 : 43ms
num2 : 41ms
num3 : 41ms
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| import matplotlib.pyplot as plt
ref="RESULT"
rep=100
l=ref.strip().split('\n')
x=[]
y1=[]
y2=[]
y3=[]
for i in range(rep):
tstr=l[i].split(",")
x.append(int(tstr[0]))
y1.append(int(tstr[1]))
y2.append(int(tstr[2]))
y3.append(int(tstr[3]))
plt.plot(x, y1, 'r', label='num1')
plt.plot(x, y2, 'g', label='num2')
plt.plot(x, y3, 'b', label='num3')
plt.xlabel('iteration')
plt.ylabel('ms')
plt.legend(loc='upper right')
plt.show()
|
Leave a comment