Two algorithms for calculating natural logarithm are more

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.

So what about the performance of the two algorithms? In the reference material [3] There are the following statements:

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

Test procedure

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 calculates the performance of two algorithms that calculate the natural logarithm by calculating ln (n!). A brief description such as the following:

    1. Line 11th Gets the n value specified by the user at the command line (if unspecified. The default value of 10) is used 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 will be executed 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 compilation and execution

Compiled and executed in the mono 2.10.8 environment of the Arch Linux 64-bit operating system, results such as the following are seen. The DecimalExtensions1.cs is the procedure in the reference material [1], DecimalExtensions2.cs is the procedure in the reference material [2], LogFactorialTest.cs is the procedure of 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 the values of ln (107!), ln (108!), and ln (109!), respectively. The longest calculation time is nearly 5.5 hours.

Compiled and executed under the Windows 7 operating system

Compile and execute 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 number 4.0.30319.17929 for Microsoft (r). NET Framework 4.5 Copyright All (C) Microsoft Corporation. All rights reserved. D:\work>Csc-out:logfactorialtest2.exe LogFactorialTest.cs DecimalExtensions2.csMicrosoft (r) Visual C # compiler version number 4.0.30319.17929 for Microsoft (r). NET Framework 4.5 Copyright All (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 executes 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.

Analysis of execution results

The above two execution results are collated as seen 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).

Execution Environment

The first machine was 2010-10-13 factory Lenovo thinkcentre m6100t PC, software and hardware information such as the following see:

The second machine model is the same as the first one, but the factory date is later. It was shipped in 2011-12-02.

The corresponding information such as the following is seen:

Two algorithms for calculating natural logarithm are more

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.