返回信息流有个函数怀疑比较耗时,就拿出来测了测
测试代码如下
while( (line=br.readLine())!=null) {
//System.out.println(line);
//executors.execute(new Handler(line));
//Thread.currentThread().sleep(100);
x++;
if(line.length() > 20000) {
//System.out.println(line);
long start = System.nanoTime();
String tt = replacePwd(line);
long end = System.nanoTime();
System.out.println(line.length()+" " +(end-start)/1000000);
}
}
要测试的函数如下:
public static String replacePwd(String log) {
String key = "(\\\\?)(\"?)(=|:)[^,^}]*(}|,)";
String value = "$2$3$1$2$1$2$4";
int length = getPASSWORDVALUELength(log,"PASSWORDVALUE");// 获取密码长度
int transferKeyLength = getPASSWORDVALUELength(log,"transferKey");// 获取密码长度
// -------------------add map toStirng replace password-------------
log = log.replaceAll("Password"+key, "Password"+value);
log = log.replaceAll("password"+key, "password"+value);
log = log.replaceAll("newPassword"+key, "newPassword"+value);
log = log.replaceAll("encryptPassword"+key, "encryptPassword"+value);
log = log.replaceAll("PSWD"+key, "PSWD"+value);
log = log.replaceAll("Passwd"+key, "Passwd"+value);
log = log.replaceAll("pinData"+key, "pinData"+value);
log = log.replaceAll("PinBlock"+key, "PinBlock"+value);
log = log.replaceAll("encryptPasswordOld"+key, "encryptPasswordOld"+value);
log = log.replaceAll("encryptPasswordNew"+key, "encryptPasswordNew"+value);
log = log.replaceAll("AuthPwd"+key, "AuthPwd"+value);//
log = log.replaceAll("SIGN_PWD"+key, "SIGN_PWD"+value);
log = log.replaceAll("\\SIGN_PWD"+key, "SIGN_PWD"+value);
log = log.replaceAll("PASSWORDVALUE"+key, "PASSWORDVALUE$1$2$3$1$2"+length+"$1$2$4");//
log = log.replaceAll("NEWPASSWORDVALUE"+key, "NEWPASSWORDVALUE"+value);
log = log.replaceAll("sqPwd"+key, "sqPwd"+value);//
log = log.replaceAll("draweeVoucherPwd"+key, "draweeVoucherPwd"+value);//
log = log.replaceAll("transferKey"+key, "transferKey$1$2$3$1$2"+transferKeyLength+"$1$2$4");//
log = log.replaceAll("plainpin"+key, "plainpin"+value);
log = log.replaceAll("cvv2"+key, "cvv2"+value);
log = log.replaceAll("cvn2"+key, "cvn2"+value);
log = log.replaceAll("cardValidityDate"+key, "cardValidityDate"+value);
log = log.replaceAll("useLife"+key, "useLife"+value);
return replacePwdFormXMl(log);
}
private static int getPASSWORDVALUELength(String log,String key) {
int length=0;
try {
String findKey = "[\\\\]?[\"]?[=|:]{1}[\\\\]?[\"]?([^\"^ ^,]*)[\\\\]?[\"]?(,|\\})";
Pattern p = Pattern.compile(key + findKey);
Matcher m = p.matcher(log);
String pwd = "";
if (m.find()) {
pwd = m.group(1);
length = pwd.length();
}
} catch (Exception e) {
}
return length;
}
public static String replacePwdFormXMl(String log) {
log = log.replaceAll("<Password>.*</Password>",
"<Password></Password>");
log = log.replaceAll("<FundTransPwd>.*</FundTransPwd>",
"<FundTransPwd></FundTransPwd>");
log = log.replaceAll("<MobilePasswd>.*</MobilePasswd>",
"<MobilePasswd></MobilePasswd>");
log = log.replaceAll("<PinBlock>.*</PinBlock>",
"<PinBlock></PinBlock>");
return log;
}
运行的结果如下:
start measuring...2016-03-05 21:55:07.708
20700 14
20182 4
20687 4
20178 1
26793 1
21062 1
33375 1
26776 1
27455 1
20166 0
20166 0
20682 1
20681 1
20171 0
30836 1
20171 0
31203 1
20171 0
35210 1
47846 1
20170 1
23483 1
20680 0
20170 0
22790 0
每次都是前几次比较慢,而且是逐渐变快,如果是第一次运行需要加载一些东西,应该是第一次后就都基本一样了,是不是因为用了JIT?我主要是怀疑这个函数处理近50000字符的字符串只要1ms,机器是2.0GHz点i7,8G 1600 DDR3内存。
这是一条镜像帖。来源:北邮人论坛 / java / #48175同步于 2016/3/5
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
统计函数运行时间时遇到的一个问题
nxlhero
2016/3/5镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
运行3遍的结果如下,主要要测的是replacePwd函数。我觉得有可能是第一次需要创建某些静态实例,所以时间长,然后越来越快是jit的原因
start measuring...2016-03-06 00:32:24.839
20700 14
20182 3
20687 3
20178 1
26793 1
21062 1
33375 1
26776 1
27455 1
20166 0
20166 0
20682 1
20681 0
20171 0
30836 1
20171 0
31203 1
20171 0
35210 1
47846 1
20170 0
23483 0
20680 0
20170 0
22790 1
start measuring...2016-03-06 00:32:25.398
20700 1
20182 0
20687 0
20178 0
26793 0
21062 0
33375 1
26776 1
27455 1
20166 0
20166 0
20682 0
20681 0
20171 0
30836 1
20171 0
31203 1
20171 0
35210 1
47846 1
20170 0
23483 1
20680 0
20170 0
22790 0
start measuring...2016-03-06 00:32:25.755
20700 0
20182 1
20687 0
20178 1
26793 1
21062 0
33375 1
26776 1
27455 1
20166 0
20166 1
20682 0
20681 0
20171 0
30836 1
20171 0
31203 1
20171 0
35210 1
47846 1
20170 0
23483 0
20680 0
20170 0
22790 0
【 在 nuanyangyang 的大作中提到: 】
: 把你的整个“测试代码”重复运行10遍呢?如果是JIT的影响,整体第二次运行的时候,函数应该早就JIT过了。
直接打印纳秒,确实是越来越快
start measuring...2016-03-06 00:41:44.604
20700 13830195
20182 2858080
20687 3884713
20178 1540711
26793 1652183
21062 1422773
33375 1407758
26776 1073417
27455 1213567
20166 891394
20166 1071529
20682 828451
20681 953467
20171 894142
30836 1196236
20171 787604
31203 1170730
20171 748065
35210 1465768
47846 1751786
20170 842312
23483 911358
20680 898343
20170 885623
22790 837338