Comparison of Math.Log and Math.Pow
I am working with annuities and have the following methods in my code:
public static double NumPMTsRemaining( double CurBalance, double ContractRate, double Pmt)
{
double rt = PeriodicRate(ContractRate);
return -1 * Math.Log(1 - (CurBalance * (rt) / Pmt)) / Math.Log(1 + (rt));
}
public static double MonthlyPMT(double OrigBalance, double ContractRate, int Term)
{
double rt = PeriodicRate(ContractRate);
if (ContractRate > 0)
return (OrigBalance * rt * Math.Pow(1 + rt, Term)) / (Math.Pow(1 + rt, Term) - 1);
else return OrigBalance / Term;
}
I use the old method to determine if the loan payment will insure the loans for the rest of my life. I use the latter method to determine if the payment is for a payment period other than monthly and then replace it with a monthly payment if so. With reflection, I can use the latter method for both tasks.
With that in mind, I was wondering if anyone would know if Math.Pow is faster / more efficient than / relative to Math.Log?
I guess Math.Pow is the best choice, but a little typing will be appreciated.
source to share
I built the test recommended by @Mangist. The code is posted below. I was surprised by @CodesInChaos answer. I did some research of course and realized that I could improve a lot of my code. I'll post a link to an interesting StackOverflow article I found in this regard. Due to the above fact, a number of people have developed improvements to Math.Pow.
Thanks again for the suggestions and information.
int term = 72;
double contractRate = 2.74 / 1200;
double balance = 20203.66;
double pmt = 304.96;
double logarithm = 0;
double power = 0;
DateTime BeginLog = DateTime.UtcNow;
for (int i = 0; i < 100000000; i++)
{
logarithm=(-1*Math.Log(1-(balance*contractRate/pmt))/Math.Log(1+contractRate));
}
DateTime EndLog = DateTime.UtcNow;
Console.WriteLine("Elapsed time= " + (EndLog - BeginLog));
Console.ReadLine();
DateTime BeginPow = DateTime.UtcNow;
for (int i = 0; i < 100000000; i++)
{
power = (balance * contractRate * Math.Pow(1 + contractRate, term)) / (Math.Pow(1
+ contractRate, term) - 1);
}
DateTime EndPow = DateTime.UtcNow;
Console.WriteLine("Elapsed time= " + (EndPow - BeginPow));
Console.ReadLine();
Benchmark results were Elapsed time for logarithm 00: 00: 04.9274927 Elapsed time for power 00: 00: 11.6981697
I also mentioned some additional StackOverflow discussions that shed some light on @CodeInChaos comment.
How is Math.Pow () implemented in the .NET Framework?
Let me add a comparison between the suggestion at the referenced link and the Math.Pow function. I was comparing Math.Pow (x, y) with Math.Exp (y * Math.Log (x)) with the following code:
DateTime PowBeginTime = DateTime.UtcNow;
for (int i = 0; i < 250000000; i++)
{
Math.Pow(1 + contractRate, term);
}
DateTime PowEndTime = DateTime.UtcNow;
Console.WriteLine("Elapsed time= " + (PowEndTime - PowBeginTime));
Console.ReadLine();
DateTime HighSchoolBeginTime = DateTime.UtcNow;
for (int i = 0; i < 250000000; i++)
{
Math.Exp(term * Math.Log(1 + contractRate));
}
DateTime HighSchoolEndTime = DateTime.UtcNow;
Console.WriteLine("Elapsed time= " + (HighSchoolEndTime - HighSchoolBeginTime));
Console.ReadLine();
Results: Math.Pow (x, y) 00: 00: 19.9469945 Math.Exp (y * Math.Log (x)) 00: 00: 18.3478346
source to share