我正在我的类之间进行一些Java性能比较,并想知道是否有某种Java性能框架可以使编写性能测量代码更容易?
也就是说,我现在正在尝试测量的是,与使用AtomicInteger作为我的"同步器"相比,PseudoRandomUsingSynch.nextInt()中的方法具有"同步"的效果.
因此,我试图测量使用3个线程访问同步方法循环10000次的生成随机整数所需的时间.
我相信有更好的方法可以做到这一点.能请你赐教吗?:)
public static void main( String [] args ) throws InterruptedException, ExecutionException {
PseudoRandomUsingSynch rand1 = new PseudoRandomUsingSynch((int)System.currentTimeMillis());
int n = 3;
ExecutorService execService = Executors.newFixedThreadPool(n);
long timeBefore = System.currentTimeMillis();
for(int idx=0; idx<100000; ++idx) {
Future<Integer> future = execService.submit(rand1);
Future<Integer> future1 = execService.submit(rand1);
Future<Integer> future2 = execService.submit(rand1);
int random1 = future.get();
int random2 = future1.get();
int random3 = future2.get();
}
long timeAfter = System.currentTimeMillis();
long elapsed = timeAfter - timeBefore;
out.println("elapsed:" + elapsed);
}
Run Code Online (Sandbox Code Playgroud)
班级
public class …Run Code Online (Sandbox Code Playgroud) Vector有两种方法可以在一个索引处获取元素.
Vector<Integer> matrix;
matrix = new Vector<Integer>;
matrix.get(0);
matrix.elementAt(0);
Run Code Online (Sandbox Code Playgroud)
看来他们在这里也做同样的事情.