java系统库性能优化注意点

对于大部分的应用系统开发来说,微秒级别的优化通常是被忽略不计或者不关心的,因为有着更多优化点,但是对于开发供应用系统使用的公用库来说,大部分的操作通常都是对字符、数字、字符串、字节的操作,且这些系统库通常调用频率很高,比如对于一个中间件来说,一次rpc请求可能涉及成百上千次的操作,所以此时细微的性能就很重要了。

final static short finalShort = 120;
static short noFinalShort = 120;
volatile static short volatileFinalShort = 120;
final static int finalInt = 120;
static int noFinalInt = 120;
volatile static int volatileFinalInt = 120;
final static long finalLong = 120;
static long noFinalLong = 120;
volatile static long volatileFinalLong = 120;
final static boolean finalBool = false;
static boolean noFinalBool = false;
volatile static boolean volatileFinalBool = false;
final static String finalStr = "dofidjfe";
static String noFinalStr = "dofidjfe";
volatile static String volatileFinalStr = "dofidjfe";
final static char finalChar = 'f';
static char noFinalChar = 'f';
volatile static char volatileFinalChar = 'f';


public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("finalShort begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(finalShort == 121) {}
}
System.out.println("finalShort end :" + System.currentTimeMillis());
System.out.println("noFinalShort begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(noFinalShort == 121) {}
}
System.out.println("noFinalShort end :" + System.currentTimeMillis());
System.out.println("volatileFinalShort begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(volatileFinalShort == 121) {}
}
System.out.println("volatileFinalShort end :" + System.currentTimeMillis());

System.out.println("finalInt begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(finalInt == 121) {}
}
System.out.println("finalInt end :" + System.currentTimeMillis());
System.out.println("noFinalInt begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(noFinalInt == 121) {}
}
System.out.println("noFinalInt end :" + System.currentTimeMillis());
System.out.println("volatileFinalInt begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(volatileFinalInt == 121) {}
}
System.out.println("volatileFinalInt end :" + System.currentTimeMillis());

System.out.println("finalLong begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(finalLong == 121) {}
}
System.out.println("finalLong end :" + System.currentTimeMillis());
System.out.println("noFinalLong begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(noFinalLong == 121) {}
}
System.out.println("noFinalLong end :" + System.currentTimeMillis());
System.out.println("volatileFinalLong begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(volatileFinalLong == 121) {}
}
System.out.println("volatileFinalLong end :" + System.currentTimeMillis());

System.out.println("finalBool begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(finalBool == true) {}
}
System.out.println("finalBool end :" + System.currentTimeMillis());
System.out.println("noFinalBool begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(noFinalBool == true) {}
}
System.out.println("noFinalBool end :" + System.currentTimeMillis());
System.out.println("volatileFinalBool begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(volatileFinalBool == true) {}
}
System.out.println("volatileFinalBool end :" + System.currentTimeMillis());

System.out.println("finalStr begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(finalStr.equals("e")) {}
}
System.out.println("finalStr end :" + System.currentTimeMillis());
System.out.println("noFinalStr begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(noFinalStr.equals("e")) {}
}
System.out.println("noFinalStr end :" + System.currentTimeMillis());
System.out.println("volatileFinalStr begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(volatileFinalStr.equals("e")) {}
}
System.out.println("volatileFinalStr end :" + System.currentTimeMillis());

System.out.println("finalChar begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(finalChar == 'e') {}
}
System.out.println("finalChar end :" + System.currentTimeMillis());
System.out.println("noFinalChar begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(noFinalChar == 'e') {}
}
System.out.println("noFinalChar end :" + System.currentTimeMillis());
System.out.println("volatileFinalChar begin:" + System.currentTimeMillis());
for(int i=0;i<100000000;i++) {
if(volatileFinalChar == 'e') {}
}
System.out.println("volatileFinalChar end :" + System.currentTimeMillis());

}

finalShort begin:1476275678790
finalShort end :1476275678837
noFinalShort begin:1476275678837
noFinalShort end :1476275678868
volatileFinalShort begin:1476275678868
volatileFinalShort end :1476275678915
finalInt begin:1476275678915
finalInt end :1476275678947
noFinalInt begin:1476275678947
noFinalInt end :1476275678978
volatileFinalInt begin:1476275678978
volatileFinalInt end :1476275679025
finalLong begin:1476275679025
finalLong end :1476275679056
noFinalLong begin:1476275679056
noFinalLong end :1476275679087
volatileFinalLong begin:1476275679087
volatileFinalLong end :1476275679119
finalBool begin:1476275679134
finalBool end :1476275679165
noFinalBool begin:1476275679165
noFinalBool end :1476275679197
volatileFinalBool begin:1476275679197
volatileFinalBool end :1476275679259
finalStr begin:1476275679259
finalStr end :1476275679681
noFinalStr begin:1476275679681
noFinalStr end :1476275680246
volatileFinalStr begin:1476275680246
volatileFinalStr end :1476275680642
finalChar begin:1476275680642
finalChar end :1476275680673
noFinalChar begin:1476275680673
noFinalChar end :1476275680735
volatileFinalChar begin:1476275680735
volatileFinalChar end :1476275680775

string相对char和整型来说,要慢5-6倍。

final除了对象外,原生类型可以在编译时达到c/c++宏的效果,因此对于执行极为频繁的操作来说,能够使用原生类型性能会好得多。

上一篇:HDU1007--Quoit Design(平面最近点对)


下一篇:微信小程序测试策略