[Mono-dev] Problem with System.Reflection.Emit

Rusmin Susanto rusminsusanto at yahoo.com
Mon Jun 5 20:32:10 EDT 2006


Hi.
  I am trying to improve performance by using System.Reflection.Emit. Here is what I do:
  - I have a class (let's call it vector). It has a 1D array of double for holding the data.
  - The Vector class has operators (+, -, etc.). In the overloaded operators, I build the expression tree. I am trying to emulate expression template trick.
  - To copy the result of calculation to the destination Vector, I define a function Assign().
  - The function Assign() will emit the opcodes based on expression that is passed. It only emit the opcodes when it's executed for the first time. The next time, it just execute the opcode.
The pseudo code of Assign() looks like this:
   
  void Assign(Expression pex)
{
 if (the opcodes don't exist)
 {
  Emit opcodes for expression pex.
 }
   execute the emitted opcodes
}
  
The main program looks like this:
  Vector v1 = new Vector(300); // create vector with 300 elements
Vector v2 = new Vector(300); // create vector with 300 elements
Vector v3 = new Vector(300); // create vector with 300 elements
Vector v4 = new Vector(300); // create vector with 300 elements
Vector v5 = new Vector(300); // create vector with 300 elements
Vector v6 = new Vector(300); // create vector with 300 elements
Vector result = new Vector(300);  // create vector with 300 elements
  result.Assign(v1 + v2 - v3 + v4 - v5 + v6);  // copy the result.
   
  To find out the performance, I execute Assign() 100000 times and measure the execution time.
   
  for(int i = 0; i < 100000; i++)
 result.Assign(v1 + v2 - v3 + v4 - v5 + v6);
   
  To compare the speed, I also define another function Assign2() and execute it also 100000 times. Here is the implementation:
   
  void Assign2(Vector v1, Vector v2, Vector v3, Vector v4, Vector v5, Vector v6)
{
 for(int i = 0; i < m_size; i++)
 {
  m_data[i] = v1.m_data[i]+v2.m_data[i]-v3.m_data[i]+v4.m_data[i]-v5.m_data[i]+v6.m_data[i];
 }
}
   
  I expect Assign (using Reflection.Emit) to outperform Assign2 because Assign() basically unrolls the loop. 
   
  But I am very surprised to find out that Assign2 is almost 3 times faster than Assign(). I tried this in on mono 1.1.13.8
  
How can this happen? I thought Reflection.Emit is superior.
Am I doing something wrong? Does it have something to do with memory or cache or ????
   
  Can someone help me?
   
  The opcodes that I emitted is pretty much the same as the opcodes in Assign2 because I monodis Assign2() and then basically "copy" the opcodes to Assign()
   
  I don't mind to go down to opcodes level as long as I can get the performance. But the result has been dissapointing so far.
   
  Thank you for your attention.
   
  
Rusmin

 __________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060605/713cc520/attachment.html 


More information about the Mono-devel-list mailing list