Project

General

Profile

Feature #12647

Make tanh faster when it lacks of HAVE_TANH

Added by qitar888 (Chia-sheng Chen) almost 3 years ago. Updated almost 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:76657]

Description

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:

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) Update-tanh-function-to-be-faster.patch qitar888 (Chia-sheng Chen), 08/02/2016 08:42 AM

Associated revisions

Revision 4483d59f
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

math.c: faster tanh

  • math.c (tanh): make faster by the extract form if three hyperbolic functions are unavailable. [Feature #12647]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55799 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 55799
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

math.c: faster tanh

  • math.c (tanh): make faster by the extract form if three hyperbolic functions are unavailable. [Feature #12647]

Revision 55799
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

math.c: faster tanh

  • math.c (tanh): make faster by the extract form if three hyperbolic functions are unavailable. [Feature #12647]

Revision 55799
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

math.c: faster tanh

  • math.c (tanh): make faster by the extract form if three hyperbolic functions are unavailable. [Feature #12647]

Revision 55799
Added by nobu (Nobuyoshi Nakada) almost 3 years ago

math.c: faster tanh

  • math.c (tanh): make faster by the extract form if three hyperbolic functions are unavailable. [Feature #12647]

History

Updated by qitar888 (Chia-sheng Chen) almost 3 years ago

  • Description updated (diff)

Updated by qitar888 (Chia-sheng Chen) almost 3 years ago

  • Description updated (diff)
#3

Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

  • Status changed from Open to Closed

Applied in changeset r55799.


math.c: faster tanh

  • math.c (tanh): make faster by the extract form if three hyperbolic functions are unavailable. [Feature #12647]

Also available in: Atom PDF