Skip to content

Commit 78518fa

Browse files
committed
Prettify benchmark in Main
1 parent 9155472 commit 78518fa

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/labs/sem1/lab10/Main.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
public class Main {
44
public static void main(String[] args) {
5-
Class<?>[] classes = { // classes to benchmark
5+
// classes to benchmark
6+
Class<?>[] classes = {
67
Stack.class,
78
SynchroStack.class,
89
SynchroStackFast.class
910
};
1011

11-
int threadsPerMethod = 2; // threads per method
12+
int threadsPerMethod = 24; // threads per method
13+
int itersPerMethod = 1000; // iterations per method
14+
15+
// initial size of stacks (to avoid empty stack errors)
16+
int initialSize = itersPerMethod * threadsPerMethod;
1217

1318
// create stacks of each class
1419
Stack[] stacks = new Stack[classes.length];
1520
for (int i = 0; i < classes.length; i++) {
1621
try {
1722
stacks[i] = (Stack) classes[i].getConstructor().newInstance();
18-
// fill stacks with 200 elements
19-
for (int j = 0; j < 200; j++) {
23+
for (int j = 0; j < initialSize; j++) {
2024
stacks[i].push(j);
2125
}
2226
} catch (Exception e) {
@@ -28,29 +32,27 @@ public static void main(String[] args) {
2832
for (Stack stack : stacks) {
2933
Runnable[] methods = {
3034
() -> { // push (write operation)
31-
for (int i = 0; i < 1000; i++) {
35+
for (int i = 0; i < itersPerMethod; i++) {
3236
stack.push(i);
3337
}
3438
},
3539
() -> { // pop (write operation)
36-
for (int i = 0; i < 1000; i++) {
40+
for (int i = 0; i < itersPerMethod; i++) {
3741
stack.pop();
3842
}
3943
},
40-
() -> { // equals (read operation)
41-
for (int i = 0; i < 10000; i++) {
44+
() -> { // equals (read operation, light)
45+
for (int i = 0; i < itersPerMethod * 20; i++) {
4246
stack.equals(stack);
4347
}
4448
},
45-
() -> { // toString (read operation)
46-
for (int i = 0; i < 100; i++) {
49+
() -> { // toString (read operation, heavy)
50+
for (int i = 0; i < itersPerMethod / 5; i++) {
4751
stack.toString();
4852
}
4953
}
5054
};
5155

52-
53-
5456
// create threads
5557
Thread[] threads = new Thread[methods.length * threadsPerMethod];
5658
for (int i = 0; i < methods.length; i++) {
@@ -60,16 +62,19 @@ public static void main(String[] args) {
6062
threads[index].setName("Thread-" + index);
6163
}
6264
}
63-
64-
System.out.print("Benchmarking " + stack.getClass().getSimpleName());
6565

66-
// start threads and measure time
66+
// print stack type
67+
System.out.print(String.format("Benchmarking: %s ", stack.getClass().getSimpleName()));
68+
69+
// start measuring time
6770
long start = System.currentTimeMillis();
6871

72+
// start threads
6973
for (Thread thread : threads) {
7074
thread.start();
7175
}
7276

77+
// wait for threads to finish
7378
for (Thread thread : threads) {
7479
try {
7580
thread.join();
@@ -78,9 +83,9 @@ public static void main(String[] args) {
7883
}
7984
}
8085

86+
// stop measuring time
8187
long end = System.currentTimeMillis();
82-
83-
System.out.println(" - took " + (end - start) + "ms");
88+
System.out.println("- took " + (end - start) + " ms");
8489
}
8590
}
8691
}

0 commit comments

Comments
 (0)