Some optimization about the code (computing ranks of a vector)?

Posted by user1748356 on Stack Overflow See other posts from Stack Overflow or by user1748356
Published on 2012-12-12T22:32:41Z Indexed on 2012/12/12 23:03 UTC
Read the original article Hit count: 361

Filed under:
|
|
|

The following code is a function (performance-critical) to compute tied ranks of a vector:

mergeSort(x,inds,ci);
//a sort function to sort vector x of length ci, also returns keys (inds) of x.

int tj=0;
double xi=x[0];

for (int j = 1; j < ci; ++j)
{
    if (x[j] > xi)
    {
        double rankvalue = 0.5 * (j - 1 + tj);
        for (int k = tj; k < j; ++k)
        {
            ranks[inds[k]]=rankvalue;
        };

        tj = j;
        xi = x[j];
    };      
};

double rankvalue = 0.5 * (ci - 1 + tj);

for (int k = tj; k < ci; ++k)
{
    ranks[inds[k]]=rankvalue;
};

The problem is, the supposed performance bottleneck mergeSort(), which is O(NlogN) is several times faster than the other part of codes (which is O(N)), which suggests there is room for huge improvment with the other part of the codes, any advices?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c