title: Why sometimes Apple Accelerate framework is slow?
body: <hr>
  
  <p>I am playing with C and Swift 3.0 code using vecLib and <a href="https://developer.apple.com/reference/accelerate" rel="nofollow">Accelerate framework</a> from Apple as  dynamic lib + my code in <strong>C lang</strong> based project and <strong>Swift</strong> playground.</p>
  
  <p>And in situation with calling Apple's wrapper from <em>framework</em> of SIMD instruction with 1 or &lt; 4 elements computation function like <code>vvcospif()</code> from <em>framework</em> is slower than simple standart <code>cos(x * PI)</code> when functions calls from loop near 1.000 times as example.</p>
  
  <p>I know about difference between <code>vvcospif()</code> and <code>cos()</code>, I should use exactly <code>vvcospif()</code> for <code>x * PI</code>.</p>
  
  <p>Example in playground, you can just copy code and run it:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import Cocoa
  import Accelerate
  
  func cosine_interpolate(alpha: Float, a: Float, b: Float) -&gt; Float {
      let ft: Float = alpha * 3.1415927;
      let f: Float = (1 - cos(ft)) * 0.5;
  
      return a + f*(b - a);
  }
  
  var start: Date = NSDate() as Date
  
  var interp: Float;
  
  for index in 0..&lt;1000 {
     interp = cosine_interpolate(alpha: 0.25, a: 1.0, b: 0.75)
  }
  
  var end = NSDate();
  var timeInterval: Double = end.timeIntervalSince(start);
  
  print("cosine_interpolate in \(timeInterval) seconds")
  
  func fast_cosine_interpolate(alpha: Float, a: Float, b: Float) -&gt; Float {
      var x: Float = alpha
      var count: Int32 = 1
  
      var result: Float = 0
      vvcospif(&amp;result, &amp;x, &amp;count)
  
      let SINSIN_HALF_X: Float = (1 - result) * 0.5;
  
      return a + SINSIN_HALF_X * (b - a);
  }
  
  start = NSDate() as Date
  
  for index in 0..&lt;1000 {
      interp = fast_cosine_interpolate(alpha: 0.25, a: 1.0, b: 0.75)
  }
  
  end = NSDate();
  timeInterval = end.timeIntervalSince(start);
  
  print("fast_cosine_interpolate in \(timeInterval) seconds")
  </code></pre>
  
  <p>My question is:</p>
  
  <h2>Why <code>vvcospif()</code> is slow in this example?</h2>
  
  <p>May be because <code>vvcospif()</code> it is wrapper under Objective-C runtime and converting data structures / copying of memory from Intel SIMD -> Objective-C -> Swift runtime is slower then tiny <code>cos()</code>?</p>
  
  <h2>I also have performance issue with C code +</h2>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;Accelerate/Accelerate.h&gt;
  
  vvcospif(resultVector, inputVector, &amp;count);
  </code></pre>
  
  <p>when <code>inputVector</code> and <code>resultVector</code> is small arrays with 1 or 2 elements or just float variable, and calls in loop with ~ 1.000.000 times.</p>
  
  <p><code>cos(x * PI)</code> computation time near 20 ms.</p>
  
  <p>and</p>
  
  <p><code>vvcospif(x)</code> with processing one <code>float</code> or <code>float array[2]</code> - computation time near 80 ms! Where is Acceleration? :)</p>
  
  <p>Yes, in Xcode I use compiler <code>-O -whole-module-optimization</code> optimisation with whole module opt. enabled.</p>