Comparison of two algorithms for calculating natural logarithm

Source: Internet
Author: User
Tags natural logarithm arch linux

Introduction

Some time ago, I wrote two essays on algorithms for calculating natural logarithms, using the elliptical θ function-arithmetic geometric averaging and Taylor series expansions-to calculate. What about the performance of the two algorithms? In reference [3] There are the following statements:

The elliptic method above is the elliptic θ function-arithmetic geometric average, and Taylor's Method 2 is the Taylor series expansion I used. It can be seen that the elliptic method occupies an absolute advantage when the calculation precision is large, but does not dominate in the calculation accuracy hours. In our application, the precision of the decimal data type to be computed is only 28 valid digits, that is, P = 28 above. It seems that in our application it should be Taylor's Method 2 dominant.

Test program

So let's write a C # program to test it, here's the LogFactorialTest.cs:

1 usingSystem;2 usingSystem.Diagnostics;3 usingskyiv.extensions;4 5 namespaceskyiv.test6 {7   Sealed classlogfactorialtest8   {9     Static voidMain (string[] args)Ten     { One       varn = (args. Length >0) ?int. Parse (args[0]) :Ten; ARun (2); - Run (n); -     } the      -     Static voidRun (intN) -     { -       varSW =stopwatch.startnew (); +       varv =logfactorial (n); - SW. Stop (); +Console.WriteLine ("{0} {1,-30}: ln ({2:n0}!)", SW. Elapsed, V, N); A     } at      -     Static decimalLogfactorial (intN) -     { -       varv =0m; -        for(vari =1; I <= N; i++) v + = (decimal) (i). Log (); -       returnv; in     } -   } to}

This program tests the performance of two algorithms that calculate the natural logarithm by calculating ln (n!). A brief description follows:

    1. The 11th line obtains the n value specified by the user in the command line arguments (if unspecified, uses the default value of 10) to calculate ln (n!).
    2. Line 12th computes ln (2!) as a warm-up so that the CLR's JIT compiles the algorithm code that is to be run first into machine code. In addition, the two algorithms for calculating the natural logarithm calculate the value of ln (2) slightly different and can also be used as a distinguishing mark.
    3. The Logfactorial method in line 24th to 29th is used to calculate ln (n!). It is calculated using ln (n!) = ln (1) + ln (2) + ... + ln (n).
    4. This procedure uses the Stopwatch class timing.
Linux operating system compile and run

Compiled and run in the mono 2.10.8 environment of the Arch Linux 64-bit operating system, the results are as follows. DecimalExtensions1.cs is the program in reference [1], DecimalExtensions2.cs is the program in reference [2], and LogFactorialTest.cs is the procedure 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 programs use these two algorithms to calculate the values of ln (107!), ln (108!), and ln (109!), which are approximately 5.5 hours in length.

Compiled and run under the Windows 7 operating system

Compile and run in the. NET Framework 4.5 Environment of the Windows 7 SP1 32-bit operating system:

D:\work>Csc-out:logfactorialtest1.exe LogFactorialTest.cs DecimalExtensions1.csMicrosoft (r) Visual C # compiler version 4.0.30319.17929 for Microsoft (r). NET Framework 4.5 Copyright (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 for Microsoft (r). NET Framework 4.5 Copyright (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!)

As you can see, the same program runs faster on this machine. The models of the two machines are the same, but the CPU of this machine is slightly higher than the previous one.

Run result analysis

The above two run results are organized as shown in the following table:

It can be seen that in our application, the algorithm 2 (elliptic θ function-arithmetic geometric average) is about a few times slower than the algorithm 1 (Taylor series expansions).

Operating Environment

The first machine is the 2010-10-13 factory Lenovo thinkcentre m6100t PC, the software and hardware information is as follows:

The second machine model is the same as the first one, but the factory date is later than 2011-12-02. The corresponding information is as follows:

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

Comparison of two algorithms for calculating natural logarithm

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.