I still doubt your performance statements. Here is the evidence. Compile and run the program below (in release mode):
using System; using System.Diagnostics; class Test { const int Iterations = 100000000; static void Main() { Stopwatch sw = Stopwatch.StartNew(); decimal d = 1.0m; long total = 0; for (int i=0; i < Iterations; i++) { long x = ConvertTo<long>(d); total += x; } sw.Stop(); Console.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds); Console.WriteLine("Total: {0}", total); } public static T ConvertTo<T>(object data) where T : struct { return (T) Convert.ChangeType(data, typeof(T)); } }
It takes 20 seconds on my laptop - to complete 100,000,000 iterations. It's hard to believe that it takes 8 seconds for your computer to complete 40 iterations.
In other words, I strongly suspect that the problem is not where you think.
source share