(silly?) speed comparisons

Discussion in 'Python' started by mk, Jul 8, 2008.

  1. mk

    mk Guest

    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<string> move_slice(vector<string> vec, int start, int stop, int dest)
    {
    int idx = stop - start;
    vector<string> frag;

    // 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<string> slice;
    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.
    mk, Jul 8, 2008
    #1
    1. Advertising

  2. mk

    Peter Otten Guest

    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
    Peter Otten, Jul 9, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damian

    XSLT speed comparisons

    Damian, Sep 27, 2006, in forum: Python
    Replies:
    21
    Views:
    1,163
  2. Laszlo Nagy
    Replies:
    1
    Views:
    641
    Mensanator
    Apr 30, 2010
  3. MRAB
    Replies:
    2
    Views:
    323
    Laszlo Nagy
    Apr 30, 2010
  4. Replies:
    3
    Views:
    101
    -berlin.de
    Jan 24, 2007
  5. Onyxx
    Replies:
    1
    Views:
    84
    Dave Angel
    Jun 13, 2013
Loading...

Share This Page