views:

251

answers:

3

Something that takes 1 second to run on Linux takes 45 seconds to run on AIX. I haven't dug directly into that code but as a test grabbed a small application that does very little from another SO question:

int main ( int argc, char **argv)
{
int i = 0;
std::vector<int> vec;
vec.push_back(6);
vec.push_back(-17);
vec.push_back(12);

for (i=0;i<100000;i++)
   vec.push_back(i);

vec.erase(vec.begin() + 1);
return 0;
}

I have an old compiler (7.0.0.10) and I cannot belive how much slower the code runs vs. the same code on g++ 4.2.

Has anyone seen this before? It would take some work to upgrade the compiler. The sample code is about 20 times slower (real time) on a system with almost no load.

Update Reqested Box Specifications:

    Number Of Processors: 8
    Processor Clock Speed: 3504 MHz
    CPU Type: 64-bit
    Kernel Type: 64-bit
    Memory Size: 63232 MB
    Good Memory Size: 63232 MB
    Platform Firmware level: EM340_041
    Firmware Version: IBM,EM340_041
    Console Login: enable
    Auto Restart: true
    Full Core: true

Output on AIX:

real    0m0.52s
user    0m0.51s
sys     0m0.00s

Output on Linux:

 0.00s real     0.01s user     0.00s system
A: 

A couple of suggestions to narrow down the problem:

  • Use time on your program and look at the system/user times, not elapsed time. That will give you a better indication.
  • put a system("date") before each of the three initial push_back statements, before the for loop, before the erase and before the return. This will show which operation is causing the problem.
  • Tell us what hardware you're running on as well. You may have a 286-class pSeries.

Then get back to us with the hard data and we can help out some more.

paxdiablo
will do in the AM
ojblass
I agree he needs to differentiate the various parts of his code somehow, but the call to system() will so far outweigh the time taken to exec the questioners code that I don't think this is a good idea. He should increase the size of his loop and time using time() or similar.
anon
That depends on how long it's taking. For his original code, it was 45 seconds so system() will not swamp it. In addition, even if the sample code only takes 10 seconds, sleep will give an indication as to where it's going slow. System(sleep) is the fastest to check but yes, if that proves inconclusive, use the C time() or one of the sub-second variants.
paxdiablo
Sorry got mired into an issue... I was missing a 0 in the code in the loop and the hardware is super expensive... the Linux box is el cheapo...
ojblass
A: 

I suspect a suboptimal memory allocation strategy. What happens if you add

vec.reserve(10000);

before the for-loop?

zvrba
Are you saying that adding one at a time makes it allocate in a strange way?
ojblass
I'm saying that it might be a reason. You can find out only by trying.
zvrba
did the trick... it runs at blazing speed now...
ojblass
So the stl implementation in gcc which is likely newer has better internal allocation strategies?
ojblass
Is that an argument for trying to optimize the code or bump up compilers? Both?
ojblass
It's an argument for using a better STL implementation. Try to make STLPort working with your compiler.
zvrba
To clarify further: it's always a good idea to call reserve() if you have a rough idea about the final size upfront. However, when a vector needs to be resized, a decent implementation will MULTIPLY the current capacity by a constant factor. Bad implementation will ADD constant number of elements, which in the long run leads to quadratic running time, and this is what you're experiencing (most probably). I'd say it's more important to upgrade your library (either on its own, or by upgrading a compiler.)
zvrba
+1  A: 

Either there is something seriously wrong with your setup, or you haven't posted the real code. The folowing executes almost instantly on a very old 900Mhz Pentium laptop with little memory:

#include <iostream>
#include <vector>
#include <ctime>
using namespace std;;

int main ( int argc, char **argv) {

    time_t now1 = time(0);
    std::vector<int> vec;
    vec.push_back(6);
    vec.push_back(-17);
    vec.push_back(12);
    for ( int i = 0; i<10000; i++) {
      vec.push_back(i);
    }

    time_t now2 = time(0);
    vec.erase(vec.begin() + 1);

    time_t now3 = time(0);
    cout << (now2 - now1) << " " << (now3 - now2)  << endl;
}

Please run this code through both compilers and report the numbers it outputs.

anon
sorry missed a 0 when doing from memory in the loop.
ojblass
it should still be almost instant. try this code (modified with the extra zero) - it will pinpoint which functions are slow, if any of them ar
anon