473,414 Members | 1,757 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,414 software developers and data experts.

(silly?) speed comparisons

mk
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.
Jul 8 '08 #1
1 1317
mk wrote:
Now C++ version took over twice the time to execute in comparison to
Python time!
Am I comparing apples to oranges?
Indeed. Where in Python you're passing references your C++ code produces
copies of the vector. For starters you can change the function signature to

void move_slice(vector<string>& vec, int start, int stop, int dest)

which should already give your C++ implementation a significant speed boost.

Peter

Jul 9 '08 #2

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...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
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...
10
by: Ali Chambers | last post by:
Hi, I've written a programme that processes stock market price data in VB.NET 2005 Express. I've optimised the code as much as possible, eg:- using arrays, not passing parameters to functions,...
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: Rajanikanth Jammalamadaka | last post by:
Try using a list instead of a vector for the C++ version. Raj On Tue, Jul 8, 2008 at 3:06 PM, mk <mrkafk@gmail.comwrote: -- "For him who has conquered the mind, the mind is the best of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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 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.