各种API性能_性能优化技巧.pdf

上传人:李医生 文档编号:8972194 上传时间:2021-01-27 格式:PDF 页数:20 大小:314.26KB
返回 下载 相关 举报
各种API性能_性能优化技巧.pdf_第1页
第1页 / 共20页
各种API性能_性能优化技巧.pdf_第2页
第2页 / 共20页
各种API性能_性能优化技巧.pdf_第3页
第3页 / 共20页
各种API性能_性能优化技巧.pdf_第4页
第4页 / 共20页
各种API性能_性能优化技巧.pdf_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《各种API性能_性能优化技巧.pdf》由会员分享,可在线阅读,更多相关《各种API性能_性能优化技巧.pdf(20页珍藏版)》请在三一文库上搜索。

1、各种API性能/编码优化技巧 温绍锦 微博 性能计数单位 1 sec = 1,000 ms 毫秒 = 1,000,000 s 微秒 = 1,000,000,000 ns 纳秒 = 1,000,000,000,000 ps 皮秒 访问一次内存(Cacheline)大约1015ns 获取当前时间API性能 操作操作 耗时耗时(nano) 说明说明 System.currentMillis() 52 在linux-2.6.18下慢10倍,大约534ns System.nano() 47 在linux-2.6.18下慢12倍,大约658ns 在linux老版本(2.6.18)中,System.curr

2、entMillis 的性能是很差的,如果你的程序中,大量调用需 要获取当前时间,请升级操作系统版本。Linux- 2.6.32版本以上都是好的. 关键在于升级操作系统关键在于升级操作系统 异常性能 操作操作 耗时耗时(nano) new Exception() 1061 new Exception(), throw, catch 1092 new Exception().getStackTrace() 5786 获取堆栈信息 Thread.getStackTrace() 6560 new Exception(), throw, catch/none fillStackTrace 23 重载为空

3、的fillStackTrace方法 Throw try cach 9 抛出的是同一个Exception 异常的开销在于fillStackTrace方法 fillStackTrace方法在Exception的构造函数中调用 try/cach/throw都是非常快的 Thread.getStackTrace很慢,和Exception.getStackTrace一样 慢 关键在于关键在于fillStackTrace和和getStackTrace这两个方法这两个方法 反射性能 操作操作 耗时耗时(nano) Class.newInstance 12 Constructor.new 13 Method.

4、invoke 4 缓存了Method Field.get 15 缓存了Field Class.forname 5601000 系统类会快一些 Class.getField 243 Class.getMethod 250 Class.getConstrcutor 100 反射的性能是很好的,只要缓存了 Class/Constructor/Method/Field Class.forname/getField/getMethod/getConstructor都很慢 关键在于缓存关键在于缓存Class/Constructor/Field/Method等元数据对象等元数据对象 字符串性能(一) 第一种

5、写法: for (int i = 0, len = str.length(); i len; +i) char ch = str.charAt(i); 第二种写法: char chars = str.toCharArray(); for (int i = 0; i Unfair Lock Fair L ock 公平性 Fair Lock synchronized Unfair Lock 结论 如果不需要多个Condition和tryLock,使用 synchronized 并发性能(四)-统计信息 通过ManagementFactory .getThreadMXBean() .getThrea

6、dInfo(currentThreadId) 来获取线程的如下信息: blockedTime blockedCount waitedTime waitedCount 通过这些信息来了解你的程序多线程之间的竞争 信息,这些数值越小越好。 对象大小 class UUID long mostSigBits; long leastSigBits; class UUID byte data; / length 16 第一种写法更节省内存 对象大小 class Stats AtomicLong count; class Stats volatile long count; static AtomicLon

7、gFieldUpdater countUpdater; long incrementAndGet() countUpdater.incrementAndGet(this); 第二种写法比较麻烦,但是更节省内存 对象大小 使用int表示IP地址 LongHashMap/ConcurrentLongHashMap 数组操作性能 当数组的长度大于8做拷贝操作时,尽量使用 System.arrayCopy提升性能。 for (int i = 0; i src.length; +i) desti = srci; 当src.length,也就是copy的数据量很小时,上面 的操作比System.array

8、Copy快。 安全API性能-随机数 算法算法 耗时耗时 nano UUID.randomUUID 2,934 secureRandom.nextBytes(new byte16) 2936 Random.nextBytes(new byte16) 76 threadLocalRandom.nextBytes(new byte16) 42 UUID.randomUUID是通过SecureRandom来实 现的,性能和SecureRandom.nextBytes基本 一致 JDK7中的ThreadLocalRandom性能非常好, 如果项目中需要,可以backport。 安全API-对称加密 算

9、法算法 KeySize 数据长度数据长度 加密耗时加密耗时(nano) 解密耗时解密耗时(nano) AES 128 1024 12,841 14,388 AES 192 1024 14,772 15,965 AES 256 1024 16,672 17,859 DES 56 1024 37,891 39,260 DESede 168 1024 113,259 114,719 Blowfish 128 1024 22,970 20,601 RC2 20 1024 41,096 30,573 AES加密算法比DES/DESede/Blowfish/RC2都要好 AES在三种密钥长度128/192

10、/256下的性能差别 不大 RSA算法性能 算法算法 KeySize 数据长度数据长度 加密耗时加密耗时(nano) 解密耗时解密耗时(nano) RSA 512 32 38,372 308,731 RSA 768 32 61,475 816,705 RSA 1024 32 102,841 1,668,865 RSA 2048 32 323,458 9,973,702 RSA 3072 32 679,590 RSA 4096 32 1,167,000 RSA 6144 32 2,486,000 RSA支持的KeySize范围很广51265536,加解密 的性能随着KeySize变大迅速下降 R

11、SA算法的解密比加密慢得多,大约一个数量级 安全API-摘要算法 算法算法 数据长度数据长度 耗时耗时(nano) MD2 1024 181,057 MD5 1024 4,747 SHA-1 1024 8,059 SHA-256 1024 13,350 SHA-384 1024 9,771 SHA-512 1024 9,800 常用的MD5和SHA性能都相当好 MD5比SHA大约慢一倍 优化技巧 尽量使用final/static 尽量调用JVM的intrinsic方法(查JDK源码的 vmSymbols.hpp文件) 分支判断,针对热点分支进行特别处理 在关键的性能处理时,if通常比switch更靠谱。 通过intel vtune等更工具看代码的热点,重点 优化调用频率高的代码

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1