java开源线程测试_JAVA多线程性能测试

[用到gprof时才知道,原来gprof只能对主线程统计耗时。manual上也没写线程相关的问题啊?不过有现成的解决方案:http://sam.zoy.org/writings/programming/gprof.html该方

import java.util.concurrent.CountDownLatch;

public class Test {

public static void main(String[] args) {

int num = 100000;

test1(num);

test2(num);

}

private static void test1(int max) {

long t1 = System.currentTimeMillis();

int n = method1(max);

long t2 = System.currentTimeMillis();

System.out.println("method1: value=" + n + ",time=" + (t2 - t1)

/ 1000.0);

}

private static int method1(int max) {

int num = 0;

for (int i = 1; i <= max; i++) {

boolean flag = true;

for (int j = 2; j < i - 1; j++) {

if (i % j == 0) {

flag = false;

break;

}

}

if (flag && i > num)

num = i;

}

return num;

}

private static int method2(int max) {

int num = 0;

for (int i = 1; i <= max; i++) {

boolean flag = true;

for (int j = 2; j < i - 1; j++) {

if (i % j == 0) {

flag = false;

break;

}

}

if (flag && i > num)

num = i;

}

return num;

}

private static void test2(int max) {

long t1 = System.currentTimeMillis();

int threadNumber = 20;//线程数,[1. 测试环境jmeter版本 :jmeter 2.4grinder的版本 : Grinder 3JAVA的版本:JDK 1.62. 测试代码Jmeter测试代码public class Sampler { publ在我的机器上20个线程效果最佳

final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);

int step = max / threadNumber;

for (int i = 0; i <= max; i += step) {

if (i - step >= 0) {

Calc calc = new Calc(i - step + 1, i, countDownLatch);

Thread thread = new Thread(calc);

thread.start();

}

}

try {

countDownLatch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

long t2 = System.currentTimeMillis();

System.out.println("method2: value=" + Calc.getVal() + ",time="

+ (t2 - t1) / 1000.0);

}

}

class Calc implements Runnable {

private static Integer val = 0;

private int min;

private int max;

private CountDownLatch cdl;

public Calc(int min, int max, CountDownLatch cdl) {

this.min = min;

this.max = max;

this.cdl = cdl;

}

public static int getVal() {

return val;

}

public void run() {

int num = 0;

for (int i = min; i <= max; i++) {

boolean flag = true;

for (int j = 2; j < i - 1; j++) {

if (i % j == 0) {

flag = false;

break;

}

}

if (flag && i > num)

num = i;

}

synchronized (val) {

if (num > val)

val = num;

}

cdl.countDown();

}

}

在我的机器上测试效果如下:

50线程

method1: value=99991,time=7.797

method2: value=99991,time=3.672

30线程

method1: value=99991,time=7.813

method2: value=99989,time=3.797

20线程

method1: value=99991,time=7.782

method2: value=99991,time=3.797

10线程

method1: value=99991,time=7.719

method2: value=99991,time=4.109

不难看出,当计算量较大时候,多线程程序可以比普通程序快80%[ 测试目的: ---------------------------------------------------------了解GraphicsMagick启用与禁用多线程(openmp),对GraphicsMagick性能的影响 编译参数使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值