Comparison of Two Algorithms for calculating natural logarithm

Source: Internet
Author: User
Tags natural logarithm arch linux
Introduction


Some time ago, I wrote two articles to calculate the natural logarithmAlgorithmUsing the elliptic θ function-arithmetic geometric mean and Taylor series expansion respectively. 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 Program


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 = 0 m;  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:


    1. 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 !).
    2. Row 3 calculates Ln (2 !) Warm up so that the clr jit will run the algorithm firstCodeCompiled as a 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.
    3. 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.
    4. 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 -- version Mono C # compiler version 2.10.8.0work $ DMCS logfactorialtest. CS decimalextensions1.cs-out: logfactorialtest1.exe Work $DMCS logfactorialtest. CS decimalextensions2.cs-out: logfactorialtest2.exe Work $ Mono logfactorialtest1.exe 10000000 00:00:00. 0022244 0.6931471805599453094172321215: ln (2 !) 00: . 709 1158 151180965.48756956489842537225: ln (10,000,000 !) Work $ Mono logfactorialtest2.exe 10000000 00:00:00. 0181903 0.6931471805599453094172321225: ln (2 !) 00: . 939 0478 151180965.48756956489842537227: ln (10,000,000 !) Work $ Mono logfactorialtest1.exe 100000000 00:00:00. 0022159 0.6931471805599453094172321215: ln (2 !) 00: . 652 9645 1742068084.5245154532285821925: ln (100,000,000 !) Work $Mono logfactorialtest2.exe 100000000 00:00:00. 0133964 0.6931471805599453094172321225: ln (2 !) 00: 39: 23.865 2797 1742068084.5245154532285821925: ln (100,000,000 !) Work $ Mono logfactorialtest1.exe 1000000000 00:00:00. 0011895 0.6931471805599453094172321215: ln (2 !) 03:04:05. 521 8954 19723265848.226982607923141006: ln (1,000,000,000 !) Work $ Mono logfactorialtest2.exe 1000000000 00:00:00. 0018197 0.6931471805599453094172321225: ln (2 !) 05:27:32. 390 9935 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> logfactorialtest1.exe logfactorialtest. CS decimalextensions1.cs Microsoft (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> out: logfactorialtest2.exe logfactorialtest. CS decimalextensions2.cs Microsoft (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 10000000 00:00:00. 0034542 0.6931471805599453094172321215: ln (2 !) 00: . 104 8788 151180965.48756956489842537224: ln (10,000,000 !) D: \ work> Logfactorialtest2 10000000 00:00:00. 0043189 0.6931471805599453094172321214: ln (2 !) 00:. 163 4292 151180965.48756956489842537225: ln (10,000,000 !) D: \ work> Logfactorialtest1 100000000 00:00:00. 0034569 0.6931471805599453094172321215: ln (2 !) 00: . 174 3684 1742068084.5245154532285821925: ln (100,000,000 !) D: \ work> Logfactorialtest2 100000000 00:00:00. 0045334 0.6931471805599453094172321214: ln (2 !) 00: . 420 1181 1742068084.5245154532285821924: ln (100,000,000 !) D: \ work> Logfactorialtest1 1000000000 00:00:00. 0035446 0.6931471805599453094172321215: ln (2 !) 01:36:06. 852 3762 19723265848.226982607923141006: ln (1,000,000,000 !) D: \ work>Logfactorialtest2 1000000000 00:00:00. 0043396 0.6931471805599453094172321214: ln (2 !) 03:05:45. 974 8574 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:





References
    1. Blog: Algorithm for calculating natural logarithm
    2. Blog: A fast algorithm for calculating natural logarithm
    3. Cinii articles: practically Fast Multiple-Precision Evaluation of log (X)


 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.