title: Xtensor: can't reach numpy performance
body: <p>I'm learning <a href="https://github.com/QuantStack/xtensor" rel="nofollow noreferrer">xtensor</a> and want to get the same or even higher performance then NumPy. But unfortunately, I can't and need help.</p>
  
  <p>I did similar benchmark as <a href="https://stackoverflow.com/questions/47240338/performance-of-xtensor-types-vs-numpy-for-simple-reduction">here</a>: </p>
  
  <p><a href="https://stackoverflow.com/questions/47240338/performance-of-xtensor-types-vs-numpy-for-simple-reduction">Performance of xtensor types vs. NumPy for simple reduction</a></p>
  
  <p>This is C++ code, where I used pybind11 and <a href="https://github.com/QuantStack/xtensor-python" rel="nofollow noreferrer">xtensor-python</a></p>
  
  <p><code>bench.cpp</code></p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  #define XTENSOR_USE_XSIMD
  #include "xtensor/xtensor.hpp"
  #include "xtensor/xfixed.hpp"
  #include "xtensor/xarray.hpp"
  #include "xtensor/xio.hpp"
  #include "xtensor/xview.hpp"
  #define FORCE_IMPORT_ARRAY                // numpy C api loading
  #include "xtensor-python/pytensor.hpp"
  #include "xtensor-python/pyarray.hpp"
  
  namespace py = pybind11;
  
  inline double sum_pytensor(xt::pytensor&lt;double, 1&gt; &amp;m)
  {
    return xt::sum(m)();
  }
  
  
  
  inline double sum_pytensor_immediate(xt::pytensor&lt;double, 1&gt; &amp;m)
  {
    return xt::sum(m, xt::evaluation_strategy::immediate)();
  }
  
  PYBIND11_MODULE(xtensor_basics, m)
  {
      xt::import_numpy();
  
      m.def("compute_xtensor", &amp;sum_pytensor);
      m.def("compute_xtensor_immediate", &amp;sum_pytensor_immediate);
  }
  </code></pre>
  
  <p>I build this with CMake</p>
  
  <p><code>CMakeLists.txt</code></p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8.12)
  project(xtensor_basics)
  
  add_definitions(-DXTENSOR_ENABLE_XSIMD) # &lt;-- does this anything?
  add_definitions(-DXTENSOR_USE_XSIMD)
  add_subdirectory(pybind11)
  pybind11_add_module(xtensor_basics bench.cpp)
  
  include_directories(/home/--user--/include)
  include_directories(/home/--user--/.miniconda3/lib/python3.7/site-packages/numpy/core/include)
  
  </code></pre>
  
  <p>and the following command: <code>cmake . &amp;&amp; make</code> which creates <code>xtensor_basics.cpython-37m-x86_64-linux-gnu.so</code></p>
  
  <p>Then I run the benchmark with this python file:
  <code>bench.py</code></p>
  
  <pre class="lang-py prettyprint-override"><code>import timeit
  
  def time_each(func_names, sizes):
      setup = f'''
  import numpy; import xtensor_basics
  arr = numpy.random.randn({sizes})
      '''
      tim = lambda func: min(timeit.Timer(f'{func}(arr)',
                                          setup=setup).repeat(3, 100))
      return [tim(func) for func in func_names]
  
  from functools import partial
  
  sizes = [10 ** i for i in range(7)]
  funcs = ['numpy.sum',
           'xtensor_basics.compute_xtensor_immediate',
           'xtensor_basics.compute_xtensor']
  sum_timer = partial(time_each, funcs)
  times = list(map(sum_timer, sizes))
  print(times)
  from matplotlib import pyplot as plt 
  
  plt.Figure(figsize=(5, 10))
  plt.plot(times)
  plt.legend(["numpy", "xtensor_immediate", "xtensor"])
  plt.show()
  </code></pre>
  
  <p>Result:</p>
  
  <p><a href="https://i.stack.imgur.com/hLGzN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hLGzN.png" alt="enter image description here"></a></p>
  
  <p><strong>Directory</strong> (after building)</p>
  
  <pre><code>bench.cpp
  bench.py
  CMakeCache.txt
  CMakeFiles
  cmake_install.cmake
  CMakeLists.txt
  Makefile
  pybind11            &lt;---clonned from the repo
  xtensor_basics.cpython-37m-x86_64-linux-gnu.so
  </code></pre>
  
  <p><strong>Include dirrectory</strong>
  All folders containing headers (I didn't build these libraries, just copied headers)</p>
  
  <pre><code>$ ls /home/--user--/include -1
     xflens
     xsimd
     xtensor
     xtensor-blas
     xtensor-python
     xtl
  </code></pre>
  
  <p><strong>System</strong></p>
  
  <pre><code>Ubuntu                         18.04
  g++                            7.4.0
  numpy                          1.16.4
  openblas                       0.2.20
  python                         3.7.3
  xtensor                        0.20.8 
  </code></pre>
  
  <p><strong>Question</strong>: What flags, definitions etc. should I add to get the same performance?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT: 1</strong>
  When I built with <code>cmake -DCMAKE_BUILD_TYPE=Release .</code>, i.e enabling optimisations, the result has improved, but still slower:
  <a href="https://i.stack.imgur.com/Oc9zq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oc9zq.png" alt="enter image description here"></a></p>