Processing an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    Processing an array

    The following code is twice as fast using 'max' rather than just 11 in the three loops. Could someone explain why this is so?
    Code:
    int main()
    {
        int const max=11;
        int count=0,num=0;
        int a[]={2,3,5,7,6,1,4,9,8,2,5};
        //print array
        cout<<"\n\n";
        cout<<"the array is: \n";
        for(int i=0;i<max;i++)
        cout<<a[i]<<" ";
        //now find which integers are repeated
        for(int i=0;i<max;i++)
         {
           for(int j=0;j<max;j++)
              {
              if(a[j]==i)
              count++;
              }
            if(count<1)continue;
            if(count>num)num=count;
            cout<<"\nthere was "<<count<<" "<<i;
            count=0;
    
        }
     cout<<"\nthe most repeats was "<<num<<endl;
    
    }
    thanks in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    How did you measure this? Because over an iteration of 11 items I would have thought the difference would either be not measurable or not significant.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I have just acquired code::blocks (am not very familiar)and it routinely publishes the execution time for a program although I`m not sure if this includes the screen writing time (hope not). In this case it said either 0.125 s(with 11) or 0.063 s (with max).

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Well I can't explain it and there seems no reason why is should be different for me.

        Compiling and running in Code:Blocks (which I just happen to have installed) gives run times of 0.003s for both cases.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          OK thanks Banfa. I will look into it further.

          Comment

          Working...