Issue #12647 has been updated by Chia-sheng Chen.
Description updated
----------------------------------------
Feature #12647: Make tanh faster when it lacks of HAVE_TANH
https://bugs.ruby-lang.org/issues/12647#change-59878
* Author: Chia-sheng Chen
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
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?
---Files--------------------------------
Update-tanh-function-to-be-faster.patch (598 Bytes)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>