Rajanikanth Jammalamadaka wrote:
Try using a list instead of a vector for the C++ version.

Well, it's even slower:

$ time slice4

real    0m4.500s
user    0m0.015s
sys     0m0.015s


Time of execution of vector version (using reference to a vector):

$ time slice2

real    0m2.420s
user    0m0.015s
sys     0m0.015s

Still slower than Python!


Source, using lists in C++:

---- slice4.c++ ----
#include <list>
#include <iostream>
#include <string>

using namespace std;

list<string> move_slice(list<string>& slist, int start, int stop, int dest)
{
        int idx;
        if( dest > stop)
                idx = dest - (stop - start);
        else
                idx = dest;
        
        list<string> frag;
        
        int i;
        list<string>::iterator startiter;
        list<string>::iterator enditer;
                
        startiter = slist.begin();

        for (i = 0; i < start; i++)
                startiter++;
        enditer = startiter;
        
        // copy fragment
        for (i = start; i < stop; i++)
        {
                frag.push_back(*enditer);
                enditer++;
        }       
        
        // delete frag from the slist
        slist.erase( startiter, enditer );
        
        // insert frag into slist at idx
        startiter = slist.begin();
        for (i = 0; i < idx; i++)
                startiter++;
        slist.insert( startiter, frag.begin(), frag.end());
        
/*      cout << "frag " << endl;
        for (startiter = frag.begin(); startiter != frag.end(); startiter ++)
                cout << *startiter << " ";
        cout << endl;
        
        cout << "slist " << endl;
        for (startiter = slist.begin(); startiter != slist.end(); startiter++)
                cout << *startiter << " ";
        cout << endl;*/
        
        return slist;
}
        

int main(int argc, char* argv)
{
        list<string> slice;
        string u = "abcdefghij";
        int pos;
        for (pos = 0; pos < u.length(); pos++)
                slice.push_back(u.substr(pos,1));
        int i;
        for (i = 0; i<1000000; i++)
                move_slice(slice, 6, 7, 7);

}

--------------------


Source, using reference to a vector:

---- slice2.c++ ----
#include <vector>
#include <iostream>
#include <string>

using namespace std;

vector<string> move_slice(vector<string>& vec, int start, int stop, int dest)
{
        int idx = stop - start;
        vector<string> frag;
        for (idx = start; idx < stop; idx++)
                frag.push_back(vec.at(idx));
        if( dest > stop)
                idx = dest - (stop - start);
        else
                idx = dest;
        vec.erase( vec.begin() + start, vec.begin() + stop);
        vec.insert( vec.begin() + idx, frag.begin(), frag.end());
        return vec;
}
        

int main(int argc, char* argv)
{
        vector<string> slice;
        string u = "abcdefghij";
        int pos;
        for (pos = 0; pos < u.length(); pos++)
                slice.push_back(u.substr(pos,1));
        int i;
        for (i = 0; i<1000000; i++)
                move_slice(slice, 6, 7, 7);

}

-----------------

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to