摘要:背景許多時(shí)候需要對(duì)比不同的框架或工具或算法,選擇使用性能更優(yōu)的那一個(gè)。通常的做法是但這樣的做法非常不嚴(yán)謹(jǐn),因?yàn)楫?dāng)獨(dú)立頻繁運(yùn)行這一小塊代碼時(shí),可能會(huì)針對(duì)性的做一些優(yōu)化工作,而在實(shí)際的生產(chǎn)環(huán)境中是不會(huì)有此優(yōu)化的。
背景
許多時(shí)候需要對(duì)比不同的框架或工具或算法, 選擇使用性能更優(yōu)的那一個(gè)。通常的做法是
long start = System.currentTimeMillis(); for(int i=0; i但這樣的做法非常不嚴(yán)謹(jǐn), 因?yàn)楫?dāng)獨(dú)立頻繁運(yùn)行這一小塊代碼時(shí),Jvm可能會(huì)針對(duì)性的做一些優(yōu)化工作, 而在實(shí)際的生產(chǎn)環(huán)境中是不會(huì)有此優(yōu)化的。 如一個(gè)Jvm優(yōu)化的例子
原始代碼
int a = 1; int b = 2; int sum = a + b; return sum;可能直接優(yōu)化為
return 3;所以最好使用更嚴(yán)謹(jǐn)?shù)墓ぞ?來進(jìn)行benchmark的工作,
Microbenchmarkings tools are intended to bring the JVM in "stable" state prior to executing tests and run your test suite for sufficient amount of time to get statistically proven evidence of the results.
摘自:http://www.buzdin.lv/2011/01/...而jmh就是這么一個(gè)工具
jmh使用示例JMH is short for Java Microbenchmark Harness. JMH is a toolkit that helps you implement Java microbenchmarks correctly. JMH is developed by the same people who implement the Java virtual machine, so these guys know what they are doing.
使用jmh來對(duì)比BeanUtils和BeanCopier
代碼
@State(Scope.Thread) public class CopyPropertiesBenchmark { private UserModel model = new UserModel(); private BeanCopier beanCopier = BeanCopier.create(UserModel.class, UserVo.class, false); /** * 人工setter復(fù)制屬性 * * @return */ @Benchmark public UserVo manuallySetter() { UserVo vo = new UserVo(); vo.setAvatar(model.getAvatar()); vo.setNick(model.getNick()); // ... return vo; } @Benchmark public UserVo beanUitls() { UserVo vo = new UserVo(); BeanUtils.copyProperties(this.model, vo); return vo; } @Benchmark public UserVo beanCopier() { UserVo vo = new UserVo(); beanCopier.copy(this.model, vo, null); return vo; } }運(yùn)行結(jié)果
# JMH 1.14.1 (released 5 days ago) # VM version: JDK 1.8.0_91, VM 25.91-b14 # VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/bin/java # VM options: -server # Warmup: 5 iterations, 1 s each # Measurement: 5 iterations, 1 s each # Timeout: 10 min per iteration # Threads: 1 thread, will synchronize iterations # Benchmark mode: Throughput, ops/time # Benchmark: com.zhugw.CopyPropertiesBenchmark.beanCopier # Run progress: 0.00% complete, ETA 00:00:30 # Fork: 1 of 1 # Warmup Iteration 1: 70737107.801 ops/s # Warmup Iteration 2: 63704495.294 ops/s # Warmup Iteration 3: 112706026.910 ops/s # Warmup Iteration 4: 113979995.875 ops/s # Warmup Iteration 5: 114595368.349 ops/s Iteration 1: 113207053.036 ops/s Iteration 2: 111641520.705 ops/s Iteration 3: 111258060.492 ops/s Iteration 4: 110293317.399 ops/s Iteration 5: 110773170.278 ops/s Result "beanCopier": 111434624.382 ±(99.9%) 4285987.844 ops/s [Average] (min, avg, max) = (110293317.399, 111434624.382, 113207053.036), stdev = 1113057.432 CI (99.9%): [107148636.538, 115720612.226] (assumes normal distribution) ... # Run complete. Total time: 00:00:32 Benchmark Mode Cnt Score Error Units CopyPropertiesBenchmark.beanCopier thrpt 5 111434624.382 ± 4285987.844 ops/s CopyPropertiesBenchmark.beanUitls thrpt 5 2451858.127 ± 525264.183 ops/s CopyPropertiesBenchmark.manuallySetter thrpt 5 103524264.901 ± 17644747.083 ops/s從上面的結(jié)果可知BeanCopier的性能優(yōu)于BeanUtils, 吞吐量差距約為45倍。
參考文檔http://tutorials.jenkov.com/j...
http://nitschinger.at/Using-J...
http://hg.openjdk.java.net/co...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66065.html
摘要:于是我建議這位小伙伴使用了進(jìn)行屬性拷貝,這為我們的程序挖了一個(gè)坑阿里代碼規(guī)約當(dāng)我們開啟阿里代碼掃描插件時(shí),如果你使用了進(jìn)行屬性拷貝,它會(huì)給你一個(gè)非常嚴(yán)重的警告。大名鼎鼎的提供的包,居然會(huì)存在性能問題,以致于阿里給出了嚴(yán)重的警告。 聲明:本文屬原創(chuàng)文章,始發(fā)于公號(hào):程序員自學(xué)之道,并同步發(fā)布于 https://blog.csdn.net/dadiyang,特此,同步發(fā)布到 sf,轉(zhuǎn)載請(qǐng)注...
摘要:前言作為一名全干打字員,干活時(shí)經(jīng)常會(huì)被要求使用各種各樣的語言去實(shí)現(xiàn)各種各樣的需求,來回切換起來寫的代碼就會(huì)或多或少有點(diǎn)不規(guī)范。今天我們以為例,講講在代碼中,我們需要注意的某些規(guī)范。 前言 作為一名全干打字員,干活時(shí)經(jīng)常會(huì)被要求使用各種各樣的語言去實(shí)現(xiàn)各種各樣的需求,來回切換起來寫的代碼就會(huì)或多或少有點(diǎn)不規(guī)范。今天我們以JAVA為例,講講在代碼中,我們需要注意的某些規(guī)范。(本文標(biāo)準(zhǔn)依賴于...
摘要:性能大比拼簡介拷貝在工作中被大量使用,可以大幅度的提高工作量。本文對(duì)常用的工具進(jìn)行了壓力測(cè)試,方便大家選擇更加適合自己的工具。本篇文章是增強(qiáng)介紹續(xù)篇,該專欄會(huì)持續(xù)更新,感興趣的朋友請(qǐng)訂閱我們。的表現(xiàn)反而比更好,可能是模型不一樣導(dǎo)致的。 Java Bean Copy 性能大比拼 簡介 Bean 拷貝在工作中被大量使用,可以大幅度的提高工作量。本文對(duì)常用的 Bean copy 工具進(jìn)行了...
閱讀 2645·2021-11-18 10:07
閱讀 1095·2021-08-03 14:04
閱讀 737·2019-08-30 13:08
閱讀 2591·2019-08-29 15:33
閱讀 1105·2019-08-29 14:07
閱讀 3005·2019-08-29 14:04
閱讀 1450·2019-08-29 11:19
閱讀 1158·2019-08-29 10:59