java与c#都支持反射,但是从网络上搜索两大阵营对于反射的态度,基本上.net开发人员都建议慎用反射,因为会有性能开销;反到是java阵营里好象在大量肆无忌惮的使用反射。于是写了下面的测试代码:
c#版的:
二个project,如上图,Model项目中就只有一个实体类Person,代码如下:
using System; namespace Model
{
public class Person
{
private int Add(object i,object j)
{
return (int)i + (int)j;
}
}
}
然后在一个Console控制台里反射Model项目生成的dll,并调用Person类的Private方法
using System;
using System.Diagnostics;
using System.Reflection; namespace ReflectionStudy
{
class Program
{
static void Main(string[] args)
{
var asm = Assembly.LoadFile(@"R:\Relection\ReflectionStudy\ReflectionStudy\bin\Release\Model.dll");
int i = , j = , limit = ;
Stopwatch watch = new Stopwatch();
watch.Reset();
watch.Start();
for (i = ; i < limit; i++)
{
j = TestReflection(asm, i);
}
watch.Stop();
Console.WriteLine("{0}次反射,平均耗时:{1}毫秒/次", limit, watch.ElapsedMilliseconds / (float)limit);
Console.WriteLine(j);
Console.Read();
} static int TestReflection(Assembly asm, int i)
{
var person = asm.CreateInstance("Model.Person");
var privateMethod = person.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.NonPublic);
return (int)privateMethod.Invoke(person, new object[] { i, });
}
}
}
运行的结果:
1000000次反射,平均耗时:0.003184毫秒/次
1000000
Java版:
如上图,同样二个project,model里就一个类Person,代码跟c#版类似:
package jimmy; public class Person {
private Integer add(Object i,Object j){
return (Integer)i + (Integer)j;
}
}
RelectionTest里引用model生成的jar包,主要代码如下:
package test; import java.lang.reflect.Method;
import java.text.DecimalFormat; public class Program {
/**
* @param args
*/
public static void main(String[] args) {
try {
Class<?> c = Class.forName("jimmy.Person");
Integer i = 0, j = 0, limit = 1000000;
long startMili = System.currentTimeMillis();
for (i = 0; i < limit; i++) {
j = testReflection(c, i);
}
long stopMili = System.currentTimeMillis(); float elapsedTime = (stopMili - startMili) / (float) limit;
DecimalFormat df1=new DecimalFormat("#0.000000"); System.out.println(limit +"次反射,平均耗时:" + df1.format(elapsedTime) + "毫秒/次");
System.out.println(j);
} catch (Exception e) {
e.printStackTrace();
}
} static Integer testReflection(Class<?> c, Integer i) {
try {
Method m = c.getMethod("add", Object.class, Object.class);
return (Integer) m.invoke(c.newInstance(), i, 1);
} catch (Exception e) {
e.printStackTrace();
}
return 0; } }
在同一台机器上的运行结果:
1000000次反射,平均耗时:0.000301毫秒/次
1000000
单就这个示例而言,java的反射效率整整高出了c#10倍,难道是我姿势不对?