473,659 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c omwrote:
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("m ove_slice.move_ slice(move_slic e.b, 4, 6, 7)", "import
move_slice ")
t.timeit()
3.8792528100638 49

(Python 2.5, Windows)

Implementing the same algorithm in C++:

#include <vector>
#include <iostream>
#include <string>

using namespace std;

vector<stringmo ve_slice(vector <stringvec, int start, int stop, int dest)
{
int idx = stop - start;
vector<stringfr ag;

// 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<stringsl ice;
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(slic e, 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("m ove_slice.move_ slice(move_slic e.b, 6, 7, 7)", "import
move_slice ")
t.timeit()
1.6097668837799 12

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 1448

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

Similar topics

4
1568
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: i.e. Python is slow compared to Perl, C++, Java. http://www.flat222.org/mac/bench/ Is there more authoritative and current information IRT speed comparisons? Is newer Python any faster?
6
6870
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 sure I'll get scolded for an OT post, but I think issues of this type should be of interest to programmers until they get a handle on them. I was reading some old threads about the relative advantages of using ++i vs i++ (I won't rehash the many...
2
620
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 the whole thing at > <ftp://ftp.eskimo.com/u/s/scs/C-faq/faq.Z> > Apparently it's the most up-to-date version available. Well its plain text, and it is contiguous, which is a lot better than my 331 html files totalling 982kB, but up to date it...
8
6273
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 one copy of the pixel data and that's it (rather than multiple copies). Every image has a unique StudyID, SeriesID, and then ImageID. So, I created a std::map that looks like this: struct ImageSink {
21
2528
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 language, etc. What I've got is: two websites, one in ASP.NET v2 and one in Python 2.5 (using 4suite for XML/XSLT) both on the same box (Windows Server 2003) both using the same XML, XSLT, CSS
0
1992
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
1
1409
by: SonyJava | last post by:
can anyone tell me the speed of C++, ease of powering, power and flexibilty PLEASE
1
1328
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 move_slice(list_arg, start, stop, dest): frag = list_arg if dest stop: idx = dest - (stop - start)
0
141
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 or C++ functions that have not been as heavily optimized. (Python is nearly 2 decades old, and several people have perused the code base looking for speedups.)
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.