(silly?) speed comparisons
mk
mrkafk at gmail.com
Wed Jul 9 10:53:39 EDT 2008
Maric Michaud wrote:
> Le Wednesday 09 July 2008 12:35:10 mk, vous avez écrit :
>> vector<string> move_slice(vector<string>& vec, int start, int stop, int
>> dest)
>
> I guess the point is to make a vector of referene to string if you don't want
> to copy string objects all around but just a word for an address each time.
>
> The signature should be :
> vector<string&> move_slice(vector<string&>& vec, int start, int stop, int
> dest)
>
> or
>
> vector<string*> move_slice(vector<string*>& vec, int start, int stop, int
> dest)
That matters too, but I just found, the main culprit was _returning the
list instead of returning the reference to list_.
The difference is staggering - some 25 sec vs 0.2 sec:
$ time slice6
real 0m0.191s
user 0m0.015s
sys 0m0.030s
#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;
int i;
list<string*>::iterator startiter;
list<string*>::iterator enditer;
list<string*>::iterator destiter;
startiter = slist.begin();
destiter = slist.begin();
for (i = 0; i < start; i++)
startiter++;
enditer = startiter;
for (i = start; i < stop; i++)
enditer++;
for (i = 0; i < dest; i++)
destiter++;
slist.splice(destiter, slist, startiter, enditer);
/* cout << "frag " << endl;
for (startiter = frag.begin(); startiter != frag.end(); startiter ++)
cout << *startiter << " ";
cout << endl;*/
/* cout << " after: ";
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 =
"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij";
int pos;
for (pos = 0; pos < u.length(); pos++)
slice.push_back(new string(u));
int i;
//for (i = 0; i<1000000; i++)
/*list<string>::iterator startiter;
cout << "before: ";
for (startiter = slice.begin(); startiter != slice.end(); startiter++)
cout << *startiter << " ";
cout << endl;*/
for (int i = 0; i<1000000; i++)
move_slice(slice, 4, 6, 7);
}
More information about the Python-list
mailing list