473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Re: (silly?) speed comparisons

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
Jul 8 '08 #1
0 1428

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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:...
6
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...
2
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...
8
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...
21
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...
0
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
1
by: SonyJava | last post by:
can anyone tell me the speed of C++, ease of powering, power and flexibilty PLEASE
1
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...
0
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
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...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.