88 lines
2.7 KiB
Java
88 lines
2.7 KiB
Java
//: concurrency/ListComparisons.java
|
|
// ©2015 MindView LLC: see Copyright.txt
|
|
// {Args: 1 10 10} (Fast verification check during build)
|
|
// Rough comparison of thread-safe List performance.
|
|
import java.util.concurrent.*;
|
|
import java.util.*;
|
|
import com.mindviewinc.util.*;
|
|
|
|
abstract class ListTest extends Tester<List<Integer>> {
|
|
ListTest(String testId, int nReaders, int nWriters) {
|
|
super(testId, nReaders, nWriters);
|
|
}
|
|
class Reader extends TestTask {
|
|
long result = 0;
|
|
void test() {
|
|
for(long i = 0; i < testCycles; i++)
|
|
for(int index = 0; index < containerSize; index++)
|
|
result += testContainer.get(index);
|
|
}
|
|
void putResults() {
|
|
readResult += result;
|
|
readTime += duration;
|
|
}
|
|
}
|
|
class Writer extends TestTask {
|
|
void test() {
|
|
for(long i = 0; i < testCycles; i++)
|
|
for(int index = 0; index < containerSize; index++)
|
|
testContainer.set(index, writeData[index]);
|
|
}
|
|
void putResults() {
|
|
writeTime += duration;
|
|
}
|
|
}
|
|
void startReadersAndWriters() {
|
|
for(int i = 0; i < nReaders; i++)
|
|
exec.execute(new Reader());
|
|
for(int i = 0; i < nWriters; i++)
|
|
exec.execute(new Writer());
|
|
}
|
|
}
|
|
|
|
class SynchronizedArrayListTest extends ListTest {
|
|
List<Integer> containerInitializer() {
|
|
return Collections.synchronizedList(
|
|
new ArrayList<>(
|
|
new CountingIntegerList(containerSize)));
|
|
}
|
|
SynchronizedArrayListTest(int nReaders, int nWriters) {
|
|
super("Synched ArrayList", nReaders, nWriters);
|
|
}
|
|
}
|
|
|
|
class CopyOnWriteArrayListTest extends ListTest {
|
|
List<Integer> containerInitializer() {
|
|
return new CopyOnWriteArrayList<>(
|
|
new CountingIntegerList(containerSize));
|
|
}
|
|
CopyOnWriteArrayListTest(int nReaders, int nWriters) {
|
|
super("CopyOnWriteArrayList", nReaders, nWriters);
|
|
}
|
|
}
|
|
|
|
public class ListComparisons {
|
|
public static void main(String[] args) {
|
|
Tester.initMain(args);
|
|
new SynchronizedArrayListTest(10, 0);
|
|
new SynchronizedArrayListTest(9, 1);
|
|
new SynchronizedArrayListTest(5, 5);
|
|
new CopyOnWriteArrayListTest(10, 0);
|
|
new CopyOnWriteArrayListTest(9, 1);
|
|
new CopyOnWriteArrayListTest(5, 5);
|
|
Tester.exec.shutdown();
|
|
}
|
|
} /* Output:
|
|
Type Read time Write time
|
|
Synched ArrayList 10r 0w 700989 0
|
|
Synched ArrayList 9r 1w 2531313 60622
|
|
readTime + writeTime = 2591935
|
|
Synched ArrayList 5r 5w 1396291 649761
|
|
readTime + writeTime = 2046052
|
|
CopyOnWriteArrayList 10r 0w 551005 0
|
|
CopyOnWriteArrayList 9r 1w 161945 123521
|
|
readTime + writeTime = 285466
|
|
CopyOnWriteArrayList 5r 5w 549580 4230146
|
|
readTime + writeTime = 4779726
|
|
*///:~
|