I am reading and writing data to and from a file. The data in the file can be floats, doubles, ints etc. The type is not known until runtime. I will refer to data type stored in the file as Tin. Data is read into or written from arrays of type Tout. This type too is not known until runtime.
The code sequence is something like this. In the Open method Tin and Tout are known, we can create read and write methods for the known data types.
Open(...)
{
MethodInfo ReadMethod = typeof(...)GetMethod("ReadGeneric").MakeGenericMethod(new Type[] {typeof(Tin), typeof(Tout)}));
}
The read write loops repeat millions of times and rely on reflection to invoke the appropriate methods as shown below.
Read loop
{
var values = (Tout[])ReadMethod.Invoke(this,new object[]{index});
process ...
}
When examining this code using a performance profiler I find that c collosal amount if time is spent just invoking the runtime read write methods.
How do I speed this up.
Yes, this is due to the fact that the reflection API is thousands of times slower than direct method calls. There are some interesting techniques to work around this however. Check out Jon Skeet's article on using delegates to cache reflection.
There is a static setup cost but once you have done that the time to invoke the delegate repeatedly is equivalent to virtual method calls.
There are also some pre-packaged frameworks to achieve the same thing.