-1

I was trying to test the difference in the performance of std::swap and vector::swap and I compiled with and without the -std=c++0x option. I have noticed about ~200ms of difference, with the program running faster when I do not include this option.

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

int main()
{
    commentator.setReportStream (cout);

    size_t nbElts = 2048;
    vector<int> v, w;

    v.resize (nbElts);
    w.reserve (nbElts);

    for (int i = 0; i < nbElts; ++i) {
        w.push_back (i);
    }

    commentator.start ("std::swap", __FUNCTION__);
    for (int i = 0; i < 10000000; ++i) {
        std::swap (v, w);
    }
    commentator.stop (MSG_DONE);

    commentator.start ("vector::swap", __FUNCTION__);
    for (int i = 0; i < 10000000; ++i) {
        v.swap (w);
    }
    commentator.stop (MSG_DONE);
    return 0;
}

The commentator object shows the running time. Why is the difference in running time? gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)

Runing time without -std=c++0x

std::swap...done (0.319952 s)
Completed activity: std::swap (r: 0.3214s, u: 0.32s, s: 0s) done

vector::swap...done (0.26396 s)
Completed activity: vector::swap (r: 0.2652s, u: 0.264s, s: 0s) done

with -std=c++0x

std::swap...done (0.548917 s)
Completed activity: std::swap (r: 0.5507s, u: 0.5489s, s: 0s) done

vector::swap...done (0.508922 s)
Completed activity: vector::swap (r: 0.5105s, u: 0.5089s, s: 0s) done
0xFF
  • 4,140
  • 7
  • 41
  • 58
  • 11
    200ms out of what? It makes a bit of a difference if the program took 210ms to run in total, or if it took 6 hours. We also don't know which version of your compiler you're using, or which flags you're compiling with. So we really can't say much more than "well, I guess your compiler generates different code when `std=c++0x` is specified, eh?" – jalf May 03 '12 at 17:45
  • 1
    How many times did you run this? The difference looks big enough to be significant, but the average over 10 runs may reveal significant variation in times. – Nathan S. May 03 '12 at 17:50
  • @NathanS. I have run it enough times actually, and I am noticing the same thing – 0xFF May 03 '12 at 17:51
  • Can you update the code to be self-contained? It doesn't compile as-is right now. – Mysticial May 03 '12 at 17:51
  • 3
    @martani_net If you're not compiling with at least `-O2`, then it's pointless to measure performance. It's like asking Usain Bolt to run with weights and without his shoes. – Mysticial May 03 '12 at 17:53
  • 4
    @Mysticial: or more accurately, like timing his run *without telling him he's supposed to run* – jalf May 03 '12 at 17:58
  • You know that std::swap is overloaded for std::vector and so it just calls std::vector::swap, right? – Jonathan Wakely May 07 '12 at 02:25

1 Answers1

8

Well, we don't know which version of G++ you're using, and we don't know what flags you specify when compiling.

But if it takes your code half a second to do a few million pointer swaps (swapping vectors in C++0x), then I think it's pretty safe to say that you're compiling without optimizations enabled. If you're benchmarking code without optimization, you get useless data, and you are wasting your time. If you care about the speed of your code, tell your compiler to generate fast code, and then measure the differences.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
jalf
  • 243,077
  • 51
  • 345
  • 550
  • 1
    The compiler would probably remove the whole loop with opts on. – Puppy May 03 '12 at 17:56
  • Actually the code is part of a library where I wouldn't really change the options it is compiled with (-O0 for instance). What I am looking for is actually to see if swap makes copies or not, but this would be a separate question I suppose – 0xFF May 03 '12 at 18:00
  • Well, that's easy enough to answer, *for this sample*. In the first part of the test, you swap `int`s. They have to be copied, there's no shortcut no way to do a destructive move operation. So in that part, they're clearly copied. In the second part, you swap vectors. Vectors *can* be moved, but does that happen? Well, look at the timings. It took half a second to swap 10 million ints. It also takes around half a second to swap 10 million vectors. That would not be possible if the vector was copied each time. Therefore, I conclude that the vectors get moved, not copied – jalf May 03 '12 at 18:04
  • I do not clearly see your comparision of 'swap 10 million ints' and 'swap 10 million vectors'! in both loops, vectors are being swapped! – 0xFF May 03 '12 at 18:09
  • Oh yeah, sorry, I misread your code :) What is the value of `nbElts`? – jalf May 03 '12 at 18:13
  • @jalf : `size_t nbElts = 2048;` o_O – ildjarn May 03 '12 at 18:15
  • @jalf I see how you are getting too excited over all of it! – 0xFF May 03 '12 at 18:19
  • Ok sorry, I clearly can't read code today ;) So if a single copy occurred per swap, we would get 10 million memory allocations and 20 billion `int` assignments in the 0.5 second the test runs (effectively writing 160 GB data to memory per second. That's more than can be realistically done on modern hardware. So again, the compiler must move the vectors for these numbers to make sense – jalf May 03 '12 at 18:21