Try using a list instead of a vector for the C++ version.
Raj
On Tue, Jul 8, 2008 at 3:06 PM, mk <mr****@gmail.comwrote:
Out of curiosity I decided to make some speed comparisons of the same
algorithm in Python and C++. Moving slices of lists of strings around seemed
like a good test case.
Python code:
def move_slice(list_arg, start, stop, dest):
frag = list_arg[start:stop]
if dest stop:
idx = dest - (stop - start)
else:
idx = dest
del list_arg[start:stop]
list_arg[idx:idx] = frag
return list_arg
b = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
>>>import timeit t = timeit.Timer("move_slice.move_slice(move_slice.b, 4, 6, 7)", "import move_slice") t.timeit()
3.879252810063849
(Python 2.5, Windows)
Implementing the same algorithm in C++:
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<stringmove_slice(vector<stringvec, int start, int stop, int dest)
{
int idx = stop - start;
vector<stringfrag;
// copy a fragment of vector
for (idx = start; idx < stop; idx++)
frag.push_back(vec.at(idx));
if( dest stop)
idx = dest - (stop - start);
else
idx = dest;
// delete the corresponding fragment of orig vector
vec.erase( vec.begin() + start, vec.begin() + stop);
// insert the frag in proper position
vec.insert( vec.begin() + idx, frag.begin(), frag.end());
return vec;
}
int main(int argc, char* argv)
{
vector<stringslice;
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, 4, 6, 7);
}
Now this is my first take at vectors in C++, so it's entirely possible that
an experienced coder would implement it in more efficient way. Still,
vectors of strings seemed like a fair choice - after all, Python version is
operating on similarly versatile objects.
But I was still rather surprised to see that C++ version took 15% longer to
execute!
(vector, 4, 6, 7)
$ time slice
real 0m4.478s
user 0m0.015s
sys 0m0.031s
Compiler: MinGW32/gcc 3.4.5, with -O2 optimization (not cygwin's gcc, which
for some reason seems to produce sluggish code).
When I changed moving the slice closer to the end of the list / vector,
Python version executed even faster:
>>>t = timeit.Timer("move_slice.move_slice(move_slice.b, 6, 7, 7)", "import move_slice") t.timeit()
1.609766883779912
C++:
(vector, 6, 7, 7)
$ time slice.exe
real 0m3.786s
user 0m0.015s
sys 0m0.015s
Now C++ version took over twice the time to execute in comparison to Python
time!
Am I comparing apples to oranges? Should the implementations be different?
Or does the MinGW compiler simply suck?
Note: it appears that speed of Python lists falls down quickly the closer to
the list beginning one deletes or inserts elements. C++ vectors do not seem
to be as heavily position-dependent.
-- http://mail.python.org/mailman/listinfo/python-list
--
"For him who has conquered the mind, the mind is the best of friends;
but for one who has failed to do so, his very mind will be the
greatest enemy."
Rajanikanth 0 1436 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: EP |
last post by:
On questions of the suitability of Python for CGI, embedded apps, etc.,
execution speed comes to mind. I previously read some comparisons which
did not show Python in a good light in this regard:...
|
by: J. Campbell |
last post by:
Hi everyone. I'm sure that this is common knowledge for many/most
here. However, it was rather illuminating for me (someone learning
c++) and I thought it might be helpful for someone else. I'm...
|
by: Paul Brown |
last post by:
Thanks for various responses - my feeling is that the cost benefit
ratio is now approaching the noise level asymptote, though there was
at least one good point :
> compressed plain-text copy of...
|
by: markww |
last post by:
Hi,
This is a continuation of a dead post. I need to make a pixel map where
I can store pixel data for multiple images. Multiple windows in my app
may render the same image, so I want to keep...
|
by: Damian |
last post by:
Hi, I'm from an ASP.NET background an am considering making the switch
to Python. I decided to develop my next project in tandem to test the
waters and everything is working well, loving the...
| |
by: robert |
last post by:
just a note - some speed comparisons :
0.60627370238398726
0.42836673376223189
0.36965815487747022
0.016557970357098384
0.15692469294117473
0.01951756438393204
|
by: SonyJava |
last post by:
can anyone tell me the speed of C++, ease of powering, power and flexibilty PLEASE
|
by: mk |
last post by:
Out of curiosity I decided to make some speed comparisons of the same
algorithm in Python and C++. Moving slices of lists of strings around
seemed like a good test case.
Python code:
def...
|
by: Terry Reedy |
last post by:
mk wrote:
If you use Python to, in effect, call well-written C functions, and most
of the computation time is spent in the C functions, then the total time
may be less than using C++ to call C...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |