[Java 工具類]測試BeanUtils.copyProperties

平時工作中很常見的一個場景是對象屬性的復(fù)制,這時我們使用最多的是兩種方式

  1. 調(diào)用引用包中的BeanUtils.copyProperties方法,調(diào)用很簡單,只需要一行代碼侈咕,還可以設(shè)置
  2. 自己寫get、set方法逐個設(shè)置

目前常用的工具類是“org.springframework.beans.BeanUtils”或“org.apache.commons.beanutils.BeanUtils”器紧,為知道那個方法效率高耀销,我寫了一個測試類,如下:

import org.junit.Before;
import org.junit.Test;
import org.omg.PortableServer.THREAD_POLICY_ID;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StopWatch;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TestW {

    OrderInfoDto source;
    int repeattime = 100000;

    @Before
    public void before() {
        source = new OrderInfoDto();
        source.setId(123L);
        source.setBundingTime(new Date());
        source.setBadJudgeState(14);
        source.setBuyerMobile("123456789");
        source.setBuyerName("測試");
        source.setTs(new Date());
        source.setMark(2);

        source.setOrderNo(12L);
        source.setCreateDate(new Date());
        source.setCreateTime(new Date());
        source.setBundingTime(new Date());
        source.setOrderCombineBeginTime(new Date());
        source.setUpdateTime(new Date());
        source.setPreFinishTime(new Date());
        source.setPickingBeginTime(new Date());
        source.setPickingEndTime(new Date());
        source.setOrderPreDeliveryTime(new Date());
    }

    @Test
    public void test0() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < repeattime ; i++) {
            OrderInfoDto target = new OrderInfoDto();
            target.setId(source.getId());
            target.setBundingTime(source.getBundingTime());
            target.setBadJudgeState(source.getBadJudgeState());
            target.setBuyerMobile(source.getBuyerMobile());
            target.setBuyerName(source.getBuyerName());
            target.setTs(source.getTs());
            target.setMark(source.getMark());

            target.setOrderNo(source.getOrderNo());
            target.setCreateDate(source.getCreateDate());
            target.setCreateTime(source.getCreateDate());
            target.setBundingTime(source.getCreateDate());
            target.setOrderCombineBeginTime(source.getCreateDate());
            target.setUpdateTime(source.getCreateDate());
            target.setPreFinishTime(source.getCreateDate());
            target.setPickingBeginTime(source.getCreateDate());
            target.setPickingEndTime(source.getCreateDate());
            target.setOrderPreDeliveryTime(source.getCreateDate());
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());
    }

    @Test
    public void testSpring() throws InterruptedException {
//        TimeUnit.SECONDS.sleep(3);

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < repeattime ; i++) {
            OrderInfoDto target = new OrderInfoDto();
            BeanUtils.copyProperties(source,target);
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());

//        TimeUnit.DAYS.sleep(1);
    }

    @Test
    public void testApache() throws InvocationTargetException, IllegalAccessException, InterruptedException {
//        TimeUnit.SECONDS.sleep(3);

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < repeattime ; i++) {
            OrderInfoDto target = new OrderInfoDto();
            org.apache.commons.beanutils.BeanUtils.copyProperties(target,source);
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeSeconds());

//        TimeUnit.DAYS.sleep(1);

    }

}

結(jié)果如下铲汪,其中java方式是使用get/set方式設(shè)置屬性值熊尉。

image.png

從結(jié)果來看使用get/set方法是最快的罐柳。spring和Apache都是用了反射機(jī)制實現(xiàn)的屬性復(fù)制。

那為什么反射會比直接使用get/set慢那么多呢狰住!我從java 官方的 javatutorials 找到了一些線索张吉。

Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.
Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.

建議: 對性能要求比較高的系統(tǒng)不要偷懶,使用get/set方式催植。

問題:在測試的時候想知道spring或Apache哪個操作花的時間比較多肮蛹,但是一直沒找到比較通用的方法,能得到某個方法的執(zhí)行時間创南。有好心的網(wǎng)友能給出答案不勝感激額伦忠!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市稿辙,隨后出現(xiàn)的幾起案子昆码,更是在濱河造成了極大的恐慌,老刑警劉巖邻储,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赋咽,死亡現(xiàn)場離奇詭異,居然都是意外死亡芥备,警方通過查閱死者的電腦和手機(jī)冬耿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事晴弃⊥鹈椋” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵爱咬,是天一觀的道長尺借。 經(jīng)常有香客問我,道長精拟,這世上最難降的妖魔是什么燎斩? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮蜂绎,結(jié)果婚禮上栅表,老公的妹妹穿的比我還像新娘。我一直安慰自己师枣,他們只是感情好怪瓶,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著践美,像睡著了一般洗贰。 火紅的嫁衣襯著肌膚如雪找岖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天敛滋,我揣著相機(jī)與錄音许布,去河邊找鬼。 笑死绎晃,一個胖子當(dāng)著我的面吹牛蜜唾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播箕昭,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼灵妨,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了落竹?” 一聲冷哼從身側(cè)響起泌霍,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎述召,沒想到半個月后朱转,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡积暖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年藤为,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片夺刑。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡缅疟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出遍愿,到底是詐尸還是另有隱情存淫,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布沼填,位于F島的核電站桅咆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏坞笙。R本人自食惡果不足惜岩饼,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望薛夜。 院中可真熱鬧籍茧,春花似錦、人聲如沸却邓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至简十,卻和暖如春檬某,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背螟蝙。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工恢恼, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人胰默。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓场斑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親牵署。 傳聞我的和親對象是個殘疾皇子漏隐,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容