Comparison of Two Algorithms for calculating natural logarithm, calculation logarithm Algorithm
Introduction
Some time ago, I wrote two essays about algorithms used to calculate the natural logarithm, using the elliptic θ function-arithmetic geometric mean and the Taylor series expansion. What is the performance of these two algorithms? The following statements are provided in reference [3:
The elliptic method above is the elliptic θ function-arithmetic geometric average method, and Taylor's method 2 is the Taylor series expansion I use. It can be seen that elliptic method occupies an absolute advantage in computing accuracy, but it is not dominant in computing accuracy hours. In our application, the precision of the decimal data type to be calculated is only 28 valid digits, that is, the above P = 28. It seems that Taylor's method 2 is dominant in our applications.
Test procedure
So let's write a C # program to test it. below is LogFactorialTest. cs:
1 using System; 2 using System.Diagnostics; 3 using Skyiv.Extensions; 4 5 namespace Skyiv.Test 6 { 7 sealed class LogFactorialTest 8 { 9 static void Main(string[] args)10 {11 var n = (args.Length > 0) ? int.Parse(args[0]) : 10;12 Run(2);13 Run(n);14 }15 16 static void Run(int n)17 {18 var sw = Stopwatch.StartNew();19 var v = LogFactorial(n);20 sw.Stop();21 Console.WriteLine("{0} {1,-30}: ln({2:N0}!)", sw.Elapsed, v, n);22 }23 24 static decimal LogFactorial(int n)25 {26 var v = 0m;27 for (var i = 1; i <= n; i++) v += ((decimal)i).Log();28 return v;29 }30 }31 }
This program calculates ln (n !) To test the performance of the two algorithms used to calculate the natural logarithm. Brief description:
- Row 3 obtains the n value specified by the user in the command line parameter (if n is not specified, the default value 10 is used) to calculate ln (n !).
- Row 3 calculates ln (2 !) Warm up so that the clr jit will first compile the algorithm code to be run as the machine code. In addition, the ln (2) values calculated by the two algorithms for natural logarithm calculation are slightly different and can also be used as a distinguishing sign.
- The LogFactorial method of lines 24th to 29 is used to calculate ln (n !) . It uses ln (n !) = Ln (1) + ln (2) +... + ln (n) for calculation.
- This program uses the Stopwatch class timing.
Compiling and running in Linux
Compile and run the 64-bit Arch Linux mono 2.10.8 environment. The result is as follows. Here, DecimalExtensions1.cs is the program in reference [1], DecimalExtensions2.cs is the program in reference [2], and LogFactorialTest. cs is the program in the previous section.
work$ dmcs --versionMono C# compiler version 2.10.8.0work$ dmcs LogFactorialTest.cs DecimalExtensions1.cs -out:LogFactorialTest1.exework$ dmcs LogFactorialTest.cs DecimalExtensions2.cs -out:LogFactorialTest2.exework$ mono LogFactorialTest1.exe 1000000000:00:00.0022244 0.6931471805599453094172321215: ln(2!)00:01:44.7091158 151180965.48756956489842537225: ln(10,000,000!)work$ mono LogFactorialTest2.exe 1000000000:00:00.0181903 0.6931471805599453094172321225: ln(2!)00:03:54.9390478 151180965.48756956489842537227: ln(10,000,000!)work$ mono LogFactorialTest1.exe 10000000000:00:00.0022159 0.6931471805599453094172321215: ln(2!)00:17:57.6529645 1742068084.5245154532285821925: ln(100,000,000!)work$ mono LogFactorialTest2.exe 10000000000:00:00.0133964 0.6931471805599453094172321225: ln(2!)00:39:23.8652797 1742068084.5245154532285821925: ln(100,000,000!)work$ mono LogFactorialTest1.exe 100000000000:00:00.0011895 0.6931471805599453094172321215: ln(2!)03:04:05.5218954 19723265848.226982607923141006: ln(1,000,000,000!)work$ mono LogFactorialTest2.exe 100000000000:00:00.0018197 0.6931471805599453094172321225: ln(2!)05:27:32.3909935 19723265848.226982607923141007: ln(1,000,000,000!)
Our test program uses these two algorithms to calculate ln (107 !) , Ln (108 !) And ln (109 !) The maximum calculation time is nearly five and a half hours.
Compile and run Windows 7 Operating Systems
Compile and run Windows 7 SP1 32-bit. NET Framework 4.5:
D: \ work>Csc-out: LogFactorialTest1.exe LogFactorialTest. cs DecimalExtensions1.csMicrosoft (R) Visual C # compiler version 4.0.30319.17929 is used for Microsoft (R). NET Framework 4.5 copyright ownership (C) Microsoft Corporation. All rights reserved. D: \ work>Csc-out: LogFactorialTest2.exe LogFactorialTest. cs DecimalExtensions2.csMicrosoft (R) Visual C # compiler version 4.0.30319.17929 is used for Microsoft (R). NET Framework 4.5 copyright ownership (C) Microsoft Corporation. All rights reserved. D: \ work>LogFactorialTest1 1000000000:00:00. 0034542 0.6931471805599453094172321215: ln (2 !) 00:01:00. 1048788 151180965.48756956489842537224: ln (10,000,000 !) D: \ work>LogFactorialTest2 1000000000:00:00. 0043189 0.6931471805599453094172321214: ln (2 !) 00:01:47. 1634292 151180965.48756956489842537225: ln (10,000,000 !) D: \ work>LogFactorialTest1 10000000000:00:00. 0034569 0.6931471805599453094172321215: ln (2 !) 00:09:21. 1743684 1742068084.5245154532285821925: ln (100,000,000 !) D: \ work>LogFactorialTest2 10000000000:00:00. 0045334 0.6931471805599453094172321214: ln (2 !) 00:18:13. 4201181 1742068084.5245154532285821924: ln (100,000,000 !) D: \ work>LogFactorialTest1 100000000000:00:00. 0035446 0.6931471805599453094172321215: ln (2 !) 01:36:06. 8523762 19723265848.226982607923141006: ln (1,000,000,000 !) D: \ work>LogFactorialTest2 100000000000:00:00. 0043396 0.6931471805599453094172321214: ln (2 !) 03:05:45. 9748574 19723265848.226982607923141006: ln (1,000,000,000 !)
It can be seen that the same program runs faster on this machine. The models of these two machines are the same, but the CPU frequency of this machine is slightly higher than that of the previous one.
Running result analysis
The preceding two running results are as follows:
It can be seen that in our application, algorithm 2 (elliptic θ function-arithmetic geometric mean) is about twice slower than algorithm 1 (Taylor series expansion.
Running Environment
The first machine is the Lenovo ThinkCentre M6100t PC from. The software and hardware information is as follows:
The model of the second ECs instance is the same as that of the first ECs instance, but the date of departure is later than that of the first ECs instance. The corresponding information is as follows:
Copyright statement: This article is the original author of the http://www.zuiniusn.com, not allowed by the blogger can not be reproduced.