Project

General

Profile

Feature #12647

Updated by qitar888 (Chia-sheng Chen) over 8 years ago

I found tanh(x) in math.c return sinh(x) / cosh(x), which function call is a waist of time. 

 So I changed to (exp(2*x) - 1) / (exp(2*x) + 1), the extract form of tanh(). 

 But if HAVE_SINH and HAVE_COSH are defined, only HAVE_TANH undefined (a weird library but possible) 
 Then the origin formulae are faster. 

 Eventually the solution is to determined whether HAVE_SINH/COSH exists, and set the formulae accordingly. 

 Testing Code: 

 
 ~~~ ruby 
 require 'benchmark/ips' 

 Benchmark.ips do |x| 
   x.config(:times => 1000, :warmup => 5) 
   x.report(RUBY_DESCRIPTION) { Math.tanh(12.345) }  
 end 
 ~~~ 


 
 Outcome: 
 ~~~ 
 ###### # Origin with HAVE_SINH, HAVE_COSH, HAVE_TANH all undef 
 Warming up -------------------------------------- 
 ruby 2.4.0dev (2016-08-02 trunk 55797) [x86_64-linux] 
                        122.226k i/100ms 
 Calculating ------------------------------------- 
 ruby 2.4.0dev (2016-08-02 trunk 55797) [x86_64-linux] 
                           1.856M (± 4.9%) i/s -        9.289M in     5.016718s 

 ###### # All tag undifined, modified tanh() 
 Warming up -------------------------------------- 
 ruby 2.4.0dev (2016-08-02 trunk 55797) [x86_64-linux] 
                        165.404k i/100ms 
 Calculating ------------------------------------- 
 ruby 2.4.0dev (2016-08-02 trunk 55797) [x86_64-linux] 
                           2.857M (± 2.5%) i/s -       14.390M in     5.039981s 

 ###### # Only HAVE_TANH undifined 
 Warming up -------------------------------------- 
 ruby 2.4.0dev (2016-08-02 trunk 55797) [x86_64-linux] 
                        205.575k i/100ms 
 Calculating ------------------------------------- 
 ruby 2.4.0dev (2016-08-02 trunk 55797) [x86_64-linux] 
                           3.732M (± 2.2%) i/s -       18.707M in     5.015756s 
 ~~~ 
 About 50% improvement when all three tags are undefined. 
 Doesn't drop when only HAVE_TANH is undefined. 

 BTW, I want to ask how to send a patch. 
 Adding a feature here and attaching my patch is enough?

Back