文章出處

java與c#都支持反射,但是從網絡上搜索兩大陣營對于反射的態度,基本上.net開發人員都建議慎用反射,因為會有性能開銷;反到是java陣營里好象在大量肆無忌憚的使用反射。于是寫了下面的測試代碼:

c#版的:

二個project,如上圖,Model項目中就只有一個實體類Person,代碼如下:

 1 using System;
 2 
 3 namespace Model
 4 {
 5     public class Person
 6     {
 7         private int Add(object i,object j)
 8         {
 9             return (int)i + (int)j;
10         }
11     }
12 }
View Code

然后在一個Console控制臺里反射Model項目生成的dll,并調用Person類的Private方法

 1 using System;
 2 using System.Diagnostics;
 3 using System.Reflection;
 4 
 5 namespace ReflectionStudy
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             var asm = Assembly.LoadFile(@"R:\Relection\ReflectionStudy\ReflectionStudy\bin\Release\Model.dll");
12             int i = 0, j = 0, limit = 1000000;
13             Stopwatch watch = new Stopwatch();
14             watch.Reset();
15             watch.Start();
16             for (i = 0; i < limit; i++)
17             {
18                 j = TestReflection(asm, i);
19             }
20             watch.Stop();
21             Console.WriteLine("{0}次反射,平均耗時:{1}毫秒/次", limit, watch.ElapsedMilliseconds / (float)limit);
22             Console.WriteLine(j);
23             Console.Read();
24         }
25 
26         static int TestReflection(Assembly asm, int i)
27         {
28             var person = asm.CreateInstance("Model.Person");
29             var privateMethod = person.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.NonPublic);
30             return (int)privateMethod.Invoke(person, new object[] { i, 1 });
31         }
32     }
33 }
View Code

運行的結果:
1000000次反射,平均耗時:0.003184毫秒/次
1000000

Java版:

如上圖,同樣二個project,model里就一個類Person,代碼跟c#版類似:

1 package jimmy;
2 
3 public class Person {
4     private Integer add(Object i,Object j){
5         return (Integer)i + (Integer)j;        
6     } 
7 }
View Code

RelectionTest里引用model生成的jar包,主要代碼如下:

 1 package test;
 2 
 3 import java.lang.reflect.Method;
 4 import java.text.DecimalFormat;
 5 
 6 public class Program {
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         try {
12             Class<?> c = Class.forName("jimmy.Person");
13             Integer i = 0, j = 0, limit = 1000000;
14             long startMili = System.currentTimeMillis();
15             for (i = 0; i < limit; i++) {
16                 j = testReflection(c, i);
17             }
18             long stopMili = System.currentTimeMillis();
19             
20             float elapsedTime = (stopMili - startMili) / (float) limit;
21             DecimalFormat df1=new DecimalFormat("#0.000000");  
22 
23             System.out.println(limit +"次反射,平均耗時:" + df1.format(elapsedTime) + "毫秒/次");
24             System.out.println(j);
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 
30     static Integer testReflection(Class<?> c, Integer i) {
31         try {
32             Method m = c.getMethod("add", Object.class, Object.class);
33             return (Integer) m.invoke(c.newInstance(), i, 1);
34         } catch (Exception e) {
35             e.printStackTrace();
36         }
37         return 0;
38 
39     }
40 
41 }
View Code

在同一臺機器上的運行結果:
1000000次反射,平均耗時:0.000301毫秒/次
1000000

單就這個示例而言,java的反射效率整整高出了c#10倍,難道是我姿勢不對?


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()