Skip to content

Weighted sum in implemented in Xtensor runs about x4 slower than native C++ #2056

Open
@davidsajman

Description

@davidsajman

Hi,

Xtensor implementation of weighted sum:
double xaverage(const pyvector& x, const pyvector& weights) {

struct timeval start, end;
gettimeofday(&start, NULL);

double res = xt::average(x, weights)[0];

gettimeofday(&end, NULL);
float rtime = (end.tv_sec-start.tv_sec)*1000.0f+(end.tv_usec-start.tv_usec)/1000.0f;
printf("weighted average run time: %f ms\n",  rtime);

return res;

}

C++ implementation of weighted sum with Pybind:
double wavg(py::array_t& vals, py::array_t& weights)
{

struct timeval start, end;
gettimeofday(&start, NULL);

auto info = vals.request();
double* v = (double*)info.ptr;
double* w = (double*)weights.request().ptr;
double sum=0;
double wsum=0;
for (int i=0; i<info.shape[0]; i++)
{
    sum+=v[i]*w[i];
    wsum+=w[i];
}
double res = sum/wsum;

gettimeofday(&end, NULL);
float rtime = (end.tv_sec-start.tv_sec)*1000.0f+(end.tv_usec-start.tv_usec)/1000.0f;
printf("wavg run time: %f ms\n",  rtime);

return res;

}

Does somebody have an idea why Xtensor is so significantly slower?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions