I just saw that the Accelerate framework has this: I have a function that does a lot of tanh calculations.
void vvatanh ( double *, const double *, const int * ); https://developer.apple.com/library/ios/documentation/Performance/Conceptual/vecLib/#//apple_ref/c/func/vvatanh
Compared to tanh in C code, is this any faster? http://www.tutorialspoint.com/c_standard_library/c_function_tanh.htm
NOTE: Your question and my original answer had some typos. To perform a tanh, you meant vvtanh
, not vvatanh
(or vatanh
as I originally wrote). vv
means "vector" (technically from the vForce library. vvatanh
is the vector base inverse hyperbolic tangent.)
First, the point that should be obvious: If vvtanh
were universally faster/better than tanh
, then tanh
would simply be implemented as vvtanh
. The fact that both exist strongly suggests that they have different purposes or trade-offs. As others have said, profiling is important, but profiling alone isn't always enough. You need to understand how the functions mean to be used, or else your profiling will tell you that Accelerate is shockingly slow and useless.
The vector functions in Accelerate are designed to efficiently work on large vectors (arrays) of numbers simultaneously. In order for them to be useful, you must structure you data correctly. Calling a vector function on a single value is generally going to be slower than the non-vector form, since the vector form necessarily has some kind of iteration logic inside of it. The compiler and standard libraries are always free to use the vector processor (and do); you should not imagine that Accelerate has access to any magical go-fast instructions that are not available elsewhere. The difference is the algorithm used, not the vector processor per se.
So, if you have a large group of numbers arranged in a C-array and need to compute tanh
on them all at once, then vvtanh
is likely a good tool and you should profile it. If nothing else, you may save the function-call overhead of iterating over tanh
(provided it's not inlined).
If your problem is not structured that way, then you should try to redesign your data structures and algorithms so that the problem can be structured that way. Most use of vectorization is about getting your data in the right form, and then making a single function call.
If you can't structure you data that way, and you would be forced to call vvtanh
many times, then it's almost certainly a lose, and the simpler tanh
is going to be better.