java反射效率

2021-05-23 23:02:50 字數 4178 閱讀 4443

測試背景:

1. 測試簡單bean(int,integer,string)的set方法

2. loop 1億次

3. 測試**盡可能避免物件的建立,**方法的呼叫,僅僅測試set方法的耗時

測試結果:

場景  本機測試結果(xp,雙核,2g) 伺服器測試結果(linux,xen虛擬機器,8核,5.5g)

方法直接呼叫 235ms 190ms

jdk method呼叫

29188ms

4633ms

jdk method呼叫(稍作優化)

5672ms

4262ms

cglib fastmethod呼叫

5390ms

2787ms

得出乙個感性的結果:

1.jdk反射效率是直接呼叫的乙個數量級,差不多20倍

2.乙個set方法的反射呼叫時間 = 4633ms / 1億 / 3次 = 0.0154us

3.cglib的fastmethod還是有優勢的

最後,附上測試**:

1 /**

2  *

3  * 本機測試結果(xp,雙核,2g):

4  * 直接呼叫(loop=1億):       235ms

5  * 反射呼叫(loop=1億):       29188ms

6  * 反射呼叫(優化)(loop=1億):  5672ms

7  * 放射呼叫(cglib)(loop=1億):5390ms

8  *

9  * 伺服器測試結果(linux xen虛擬機器,5.5g記憶體;8核cpu):

10  * 直接呼叫(loop=1億):       190ms

11  * 反射呼叫(loop=1億):       4633ms

12  * 反射呼叫(優化)(loop=1億):  4262ms

13  * 放射呼叫(cglib)(loop=1億):2787ms

14  *

15  *

16  * @author stone.j 2010-9-15 上午10:07:27

17  */

18 public class reflectiontest ;

24     private static final object                 default_integers           = new integer ;

25     private static final object                 default_strings            = new string ;

26 27     private static final bean                     bean                       = new bean();

28 29     private static final cachedmethod             cached_method              = new cachedmethod();

30     private static final optimizationcachedmethod optimization_cached_method = new optimizationcachedmethod();

31     private static final cglibcachedmethod        cglib_cached_method        = new cglibcachedmethod();

32 33     private static final long                     loop                       = 1 * 10000 * 10000;

34 35     // 測試main

36     public static void main(string args)

41         int tc = integer.valueof(args[0]);

42 43         long start = system.currenttimemillis();

44         for (long i = 0; i < loop; i++)

66         }

67         long dur = system.currenttimemillis() - start;

68         system.out.println(dur);

69     }

70 71     // 直接呼叫測試

72     public static void test()

77 78     // 反射呼叫測試

79     public static void testreflection() catch (exception e)

87     }

88 89     // 優化後反射呼叫測試

90     public static void testoptimizationreflection() catch (exception e)

98     }

99 100     // cglib反射呼叫測試

101     public static void testcglibreflection() catch (exception e)

109     }

110

111     /**

112      *

113      * 測試的bean

114      * 簡單的int integer string型別

115      *

116      *

117      * @author stone.j 2010-9-15 上午10:40:40

118      */

119     public static class bean

128

129         public void setid(int id)

132

133         public integer getcode()

136

137         public void setcode(integer code)

140

141         public string getname()

144

145         public void setname(string name)

148

149     }

150

151     /**

152      * 反射測試需要:cached method

153      *

154      * @author stone.j 2010-9-15 上午10:41:04

155      */

156     public static class cachedmethod catch (exception e)

170         }

171

172     }

173

174     /**

175      * 反射測試需要:優化後的cached method

176      *

177      * @author stone.j 2010-9-15 上午10:41:21

178      */

179     public static class optimizationcachedmethod extends cachedmethod

187

188     }

189

190     /**

191      * 反射測試需要,使用cglib的fast method

192      *

193      * @author stone.j 2010-9-15 上午10:51:53

194      */

195     public static class cglibcachedmethod extends cachedmethod

208

209     }

210

211 }

如何提高使用Java反射的效率

前言 在我們平時的工作或者面試中,都會經常遇到 反射 這個知識點,通過 反射 我們可以動態的獲取到物件的資訊以及靈活的呼叫物件方法等,但是在使用的同時又伴隨著另一種聲音的出現,那就是 反射 很慢,要少用。難道反射真的很慢?那跟我們平時正常建立物件呼叫方法比慢多少?估計很多人都沒去測試過,只是 道聽途...

反射效率問題

反射帶來了程式設計的靈活性,但是他的執行效率相比於常規呼叫要低。禁用安全檢查可以稍微緩解 所以最好是在必須得用反射的情況下再用反射。以如下 為例,通過兩種方式呼叫某個方法 1000000000l次 privatestaticvoidtest1 privatestaticvoidtest2 throw...

java反射 通用的java反射工具

因專案需要,需要反射呼叫安卓系統audiomanager的一些方法,這些方法或者使用 hide標記,或者需要的sdk版本比較高,無法直接呼叫,這個時候,反射就是一大神器了。正常的反射呼叫流程如下 非靜態有返回值方法反射 class clazz class.forname full classname...