letsBeePolite
letsBeePolite

Reputation: 2251

Huge Run time difference in Ubuntu and Windows?

I am running the following piece of code on CodeBlocks. On both platform Windows and Ubuntu I have installed the latest CodeBlocks available.

But when I execute the following code, the run time of program differs immensely. For ex, When I set K = 1000 (#Number of inputs) then on both Platform the run time is 2.7sec (Shown by the CodeBlocks console) but when K is changed to 10000 then on Windows its around 24sec and in Ubuntu its 257sec. Can anyone suggest what could be going wrong?

 #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <vector>

    using namespace std;

    #define denominator 2
    #define getDist(a,b) exp(sqrt((a-b)*(a-b))/denominator)

    int main() {
        cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
        int K = 10000;
        int m = 10;
        //float data[4] = {1,2,3,4};
        //float dist[K*(K-1)/2];
        std::vector<float> dist(m*K);
        int count = 0;
        std::vector<float> check(m*K);

        for (int t=0;t<m*K;t++)
        {
            check[t] = (float)(rand() % 5);
            dist[t] = 0;
        }
    cout<<"Generated";
    for(int i=0; i<m*K; i++){
            for(int j =i+1;j<m*K;j++){
                int l = getDist(check[i],check[j]);

            }
        }


        return 0;
    }

Upvotes: 3

Views: 137

Answers (1)

tweej
tweej

Reputation: 842

You're not doing anything "wrong". To get similar results on Linux as you describe for Windows, turn on optimization as suggested by ChronoTrigger.

Note that the math functions rand(), exp(), and sqrt() are almost certainly implemented differently by the two OSes (compilers/libraries).

$ g++ -O3 -pg test.cc ; time ./a.out
!!!Hello World!!!
Generated
real    0m31.878s

gprof output:
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 41.31      0.96     0.96                             main
 23.67      1.51     0.55 200000000     0.00     0.00  std::vector<float, std::allocator<float> >::operator[](unsigned long)
 23.67      2.07     0.55 49995000     0.00     0.00  std::sqrt(float)
  5.16      2.19     0.12 49995000     0.00     0.00  std::exp(float)
  1.94      2.23     0.05        2    22.56    22.56  std::vector<float, std::allocator<float> >::~vector()
...

Upvotes: 1

Related Questions