Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Simple big-looped program break down
Forum Updated to NodeBB v4.3 + New Features

Simple big-looped program break down

Scheduled Pinned Locked Moved Solved C++ Gurus
big loopbreak down
38 Posts 6 Posters 11.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JonB
    21 Nov 2019, 11:31

    @Please_Help_me_D said in Simple big-looped program break down:

        int i = 0;
        while (it != a.end())
        {
            *it = i++;
            it++;
        }
    
    
    P Offline
    P Offline
    Please_Help_me_D
    wrote on 21 Nov 2019, 11:51 last edited by
    #23

    @JonB So the execution time for the code:

        QVector<int> a(100000000);
        QVector<int>::iterator it = a.begin();
        int i = 0;
    
        clock_t start_time =  clock(); // начальное время
        while (it != a.end())
        {
            *it = i++;
            it++;
        }
        clock_t end_time = clock(); // конечное время
        clock_t d_time = end_time - start_time;
        cout << d_time << endl;
    

    is 8-9 seconds
    By the way, the same operation in Matlab with single (float) precision accuracy takes 0.3 seconds. That means that Matlab loops not so slow as I thought:

    // Matlab code
    a = single(zeros(100000000,1)); // preallocate the memory
    tic // start time
    for n = 1:100000000
        a(n) = single(n); // assign avlue to each position
    end
    toc // end time
    
    J J 2 Replies Last reply 21 Nov 2019, 11:54
    0
    • P Please_Help_me_D
      21 Nov 2019, 11:51

      @JonB So the execution time for the code:

          QVector<int> a(100000000);
          QVector<int>::iterator it = a.begin();
          int i = 0;
      
          clock_t start_time =  clock(); // начальное время
          while (it != a.end())
          {
              *it = i++;
              it++;
          }
          clock_t end_time = clock(); // конечное время
          clock_t d_time = end_time - start_time;
          cout << d_time << endl;
      

      is 8-9 seconds
      By the way, the same operation in Matlab with single (float) precision accuracy takes 0.3 seconds. That means that Matlab loops not so slow as I thought:

      // Matlab code
      a = single(zeros(100000000,1)); // preallocate the memory
      tic // start time
      for n = 1:100000000
          a(n) = single(n); // assign avlue to each position
      end
      toc // end time
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 21 Nov 2019, 11:54 last edited by
      #24

      @Please_Help_me_D
      If anything, than you should take away from my test, that there's a difference between release and debug builds in c++

      build and run your test in release, it will drop way below 1 sec


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      P 1 Reply Last reply 21 Nov 2019, 12:06
      3
      • P Please_Help_me_D
        21 Nov 2019, 11:51

        @JonB So the execution time for the code:

            QVector<int> a(100000000);
            QVector<int>::iterator it = a.begin();
            int i = 0;
        
            clock_t start_time =  clock(); // начальное время
            while (it != a.end())
            {
                *it = i++;
                it++;
            }
            clock_t end_time = clock(); // конечное время
            clock_t d_time = end_time - start_time;
            cout << d_time << endl;
        

        is 8-9 seconds
        By the way, the same operation in Matlab with single (float) precision accuracy takes 0.3 seconds. That means that Matlab loops not so slow as I thought:

        // Matlab code
        a = single(zeros(100000000,1)); // preallocate the memory
        tic // start time
        for n = 1:100000000
            a(n) = single(n); // assign avlue to each position
        end
        toc // end time
        
        J Offline
        J Offline
        JonB
        wrote on 21 Nov 2019, 12:01 last edited by JonB
        #25

        @Please_Help_me_D
        Please do as @J-Hilk has said before, if you're compiling or running for debug there will be a vast difference from release/optimized

        Also separately, verify what the MATLAB does in your code with

        for n = 1:100000001

        P 1 Reply Last reply 21 Nov 2019, 12:11
        2
        • J J.Hilk
          21 Nov 2019, 11:54

          @Please_Help_me_D
          If anything, than you should take away from my test, that there's a difference between release and debug builds in c++

          build and run your test in release, it will drop way below 1 sec

          P Offline
          P Offline
          Please_Help_me_D
          wrote on 21 Nov 2019, 12:06 last edited by
          #26

          @J-Hilk sorry I forgot that!

              int *pa = new int [100000000];
              clock_t start_time =  clock(); 
              for (int i = 0; i < 100000000; i++)
              {
                  pa[i] = i;
              }
              clock_t end_time = clock(); 
              clock_t d_time = end_time - start_time;
              cout << d_time << endl;
          

          is 0.14 second

              QVector<int> a(100000000);
          
              clock_t start_time =  clock();
              for (int i = 0; i < 100000000; i++)
              {
                  a[i] = i;
              }
              clock_t end_time = clock(); 
              clock_t d_time = end_time - start_time;
              cout << d_time << endl;
          

          is also about 0.14 second

              QVector<int> a(100000000);
              QVector<int>::iterator it = a.begin(); /
              int i = 0;
          
              clock_t start_time =  clock(); 
              while (it != a.end())
              {
                  *it = i++;
                  it++;
              }
              clock_t end_time = clock(); 
              clock_t d_time = end_time - start_time; 
              cout << d_time << endl;
          

          is also about 0.14 second
          So Matlab loops is about twice slower :)
          Thank you very much! It's good to know such difference in perfomance in Debug and Release mode

          1 Reply Last reply
          2
          • J JonB
            21 Nov 2019, 12:01

            @Please_Help_me_D
            Please do as @J-Hilk has said before, if you're compiling or running for debug there will be a vast difference from release/optimized

            Also separately, verify what the MATLAB does in your code with

            for n = 1:100000001

            P Offline
            P Offline
            Please_Help_me_D
            wrote on 21 Nov 2019, 12:11 last edited by Please_Help_me_D
            #27

            @JonB Matlab automatically increases the vector size by 1. But if I dont preallocate the size of the vector and with each iteration the vector size increases then it takes way much time. Here I do the iteration without preallocation and variable "a" is created when first iteration is performed:

            // Matlab code
            tic
            for n = 1:100000000
                   a(n) = single(n);
            end
            toc
            

            35 seconds to preform

            J 1 Reply Last reply 21 Nov 2019, 12:24
            0
            • P Please_Help_me_D
              21 Nov 2019, 12:11

              @JonB Matlab automatically increases the vector size by 1. But if I dont preallocate the size of the vector and with each iteration the vector size increases then it takes way much time. Here I do the iteration without preallocation and variable "a" is created when first iteration is performed:

              // Matlab code
              tic
              for n = 1:100000000
                     a(n) = single(n);
              end
              toc
              

              35 seconds to preform

              J Offline
              J Offline
              JonB
              wrote on 21 Nov 2019, 12:24 last edited by JonB
              #28

              @Please_Help_me_D
              For presumably similar slowness with QVector, try creating it empty with just QVector<int> a and use a.append(i) in the loop. I anticipate some slowness compared to pre-sizing, but (hopefully) not as bad as it could be:

              This operation is relatively fast, because QVector typically allocates more memory than necessary, so it can grow without reallocating the entire vector each time.

              P 1 Reply Last reply 21 Nov 2019, 13:14
              1
              • J JonB
                21 Nov 2019, 12:24

                @Please_Help_me_D
                For presumably similar slowness with QVector, try creating it empty with just QVector<int> a and use a.append(i) in the loop. I anticipate some slowness compared to pre-sizing, but (hopefully) not as bad as it could be:

                This operation is relatively fast, because QVector typically allocates more memory than necessary, so it can grow without reallocating the entire vector each time.

                P Offline
                P Offline
                Please_Help_me_D
                wrote on 21 Nov 2019, 13:14 last edited by
                #29

                @JonB very interesting result as for me:

                     QVector<int> a;
                
                     clock_t start_time =  clock();
                     for (int i = 0; i < 100000000; i++)
                     {
                         a.append(i);
                     }
                     clock_t end_time = clock();
                     clock_t d_time = end_time - start_time;
                     cout << d_time << endl;
                

                in Debug mode it takes 5.5-6 seconds
                in Release mode it takes 0.7 second (while in Matlab as I previously said it takes 35 seconds)
                So QVectror is really fast even if it works with <appending mode>
                Thank you for this example!

                J 1 Reply Last reply 21 Nov 2019, 13:15
                0
                • P Please_Help_me_D
                  21 Nov 2019, 13:14

                  @JonB very interesting result as for me:

                       QVector<int> a;
                  
                       clock_t start_time =  clock();
                       for (int i = 0; i < 100000000; i++)
                       {
                           a.append(i);
                       }
                       clock_t end_time = clock();
                       clock_t d_time = end_time - start_time;
                       cout << d_time << endl;
                  

                  in Debug mode it takes 5.5-6 seconds
                  in Release mode it takes 0.7 second (while in Matlab as I previously said it takes 35 seconds)
                  So QVectror is really fast even if it works with <appending mode>
                  Thank you for this example!

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 21 Nov 2019, 13:15 last edited by
                  #30

                  @Please_Help_me_D said in Simple big-looped program break down:

                  So QVectror is really fast even if it works with <appending mode>

                  As far as I know QVector allocates more memory as currently is need and on resizing it allocates again more than "current_size + 1".

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  P J 2 Replies Last reply 21 Nov 2019, 13:49
                  0
                  • J jsulm
                    21 Nov 2019, 13:15

                    @Please_Help_me_D said in Simple big-looped program break down:

                    So QVectror is really fast even if it works with <appending mode>

                    As far as I know QVector allocates more memory as currently is need and on resizing it allocates again more than "current_size + 1".

                    P Offline
                    P Offline
                    Please_Help_me_D
                    wrote on 21 Nov 2019, 13:49 last edited by
                    #31

                    @jsulm yes it does.
                    I tested how much my program weigh with Windows Task Manager for different number of loops (say n) and two mode of QVector (normal and <append>). Here is the result (Debug mode):

                        QVector<int> a(100000000);
                    
                        clock_t start_time =  clock();
                        for (int i = 0; i < 100000000; i++)
                        {
                            a[i] = i;
                        }
                        clock_t end_time = clock();
                        clock_t d_time = end_time - start_time; 
                        cout << d_time << endl;
                    

                    n = 100000000 -> 392464 kilobytes
                    n = 100000000/2 -> 196780 kilobytes
                    n = 100000000/4 -> 98936 kilobytes
                    n = 100000000/8 -> 50008 kilobytes
                    !_____________________________!

                         QVector<int> a;
                    
                         clock_t start_time =  clock();
                         for (int i = 0; i < 100000000; i++)
                         {
                             a.append(i);
                         }
                         clock_t end_time = clock(); 
                         clock_t d_time = end_time - start_time; 
                         cout << d_time << endl;
                    

                    n = 100000000 -> 527456 kilobytes
                    n = 100000000/2 -> 264804 kilobytes
                    n = 100000000/4 -> 133468 kilobytes
                    n = 100000000/8 -> 67804 kilobytes

                    If I use simple C++ array insead of QVector then for n = 100000000 -> 391780 kilobytes (almost the same as simple QVector).

                    So the conclusion I see is that QVector in <append> mode weighs 1.35 times simple QVector or C++ array

                    J 1 Reply Last reply 21 Nov 2019, 13:52
                    0
                    • P Please_Help_me_D
                      21 Nov 2019, 13:49

                      @jsulm yes it does.
                      I tested how much my program weigh with Windows Task Manager for different number of loops (say n) and two mode of QVector (normal and <append>). Here is the result (Debug mode):

                          QVector<int> a(100000000);
                      
                          clock_t start_time =  clock();
                          for (int i = 0; i < 100000000; i++)
                          {
                              a[i] = i;
                          }
                          clock_t end_time = clock();
                          clock_t d_time = end_time - start_time; 
                          cout << d_time << endl;
                      

                      n = 100000000 -> 392464 kilobytes
                      n = 100000000/2 -> 196780 kilobytes
                      n = 100000000/4 -> 98936 kilobytes
                      n = 100000000/8 -> 50008 kilobytes
                      !_____________________________!

                           QVector<int> a;
                      
                           clock_t start_time =  clock();
                           for (int i = 0; i < 100000000; i++)
                           {
                               a.append(i);
                           }
                           clock_t end_time = clock(); 
                           clock_t d_time = end_time - start_time; 
                           cout << d_time << endl;
                      

                      n = 100000000 -> 527456 kilobytes
                      n = 100000000/2 -> 264804 kilobytes
                      n = 100000000/4 -> 133468 kilobytes
                      n = 100000000/8 -> 67804 kilobytes

                      If I use simple C++ array insead of QVector then for n = 100000000 -> 391780 kilobytes (almost the same as simple QVector).

                      So the conclusion I see is that QVector in <append> mode weighs 1.35 times simple QVector or C++ array

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 21 Nov 2019, 13:52 last edited by
                      #32

                      @Please_Help_me_D said in Simple big-looped program break down:

                      So the conclusion I see is that QVector in <append> mode weighs 1.35 times simple QVector or C++ array

                      IIRC, than its 1.5, same as std::vector


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1
                      • J jsulm
                        21 Nov 2019, 13:15

                        @Please_Help_me_D said in Simple big-looped program break down:

                        So QVectror is really fast even if it works with <appending mode>

                        As far as I know QVector allocates more memory as currently is need and on resizing it allocates again more than "current_size + 1".

                        J Offline
                        J Offline
                        JonB
                        wrote on 21 Nov 2019, 14:10 last edited by
                        #33

                        @jsulm said in Simple big-looped program break down:

                        @Please_Help_me_D said in Simple big-looped program break down:

                        So QVectror is really fast even if it works with <appending mode>

                        As far as I know QVector allocates more memory as currently is need and on resizing it allocates again more than "current_size + 1".

                        Which is why I originally quoted from https://doc.qt.io/qt-5/qvector.html#append:

                        This operation is relatively fast, because QVector typically allocates more memory than necessary, so it can grow without reallocating the entire vector each time.

                        As for the exact timing: if, say, it re-allocates by powers of 2, I make that something log2(100000000) == 27 reallocations? :)

                        K 1 Reply Last reply 22 Nov 2019, 20:07
                        0
                        • J JonB
                          21 Nov 2019, 14:10

                          @jsulm said in Simple big-looped program break down:

                          @Please_Help_me_D said in Simple big-looped program break down:

                          So QVectror is really fast even if it works with <appending mode>

                          As far as I know QVector allocates more memory as currently is need and on resizing it allocates again more than "current_size + 1".

                          Which is why I originally quoted from https://doc.qt.io/qt-5/qvector.html#append:

                          This operation is relatively fast, because QVector typically allocates more memory than necessary, so it can grow without reallocating the entire vector each time.

                          As for the exact timing: if, say, it re-allocates by powers of 2, I make that something log2(100000000) == 27 reallocations? :)

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 22 Nov 2019, 20:07 last edited by
                          #34

                          @JonB said in Simple big-looped program break down:

                          As for the exact timing: if, say, it re-allocates by powers of 2, I make that something log2(100000000) == 27 reallocations?

                          It doesn't. If memory serves me it has a special progression for the first 128 elements or so, and after that it doubles its size on each realloc.

                          Read and abide by the Qt Code of Conduct

                          J 1 Reply Last reply 23 Nov 2019, 10:12
                          1
                          • K kshegunov
                            22 Nov 2019, 20:07

                            @JonB said in Simple big-looped program break down:

                            As for the exact timing: if, say, it re-allocates by powers of 2, I make that something log2(100000000) == 27 reallocations?

                            It doesn't. If memory serves me it has a special progression for the first 128 elements or so, and after that it doubles its size on each realloc.

                            J Offline
                            J Offline
                            JonB
                            wrote on 23 Nov 2019, 10:12 last edited by JonB
                            #35

                            @kshegunov
                            When I wrote this for a realloc() replacement/interface many years ago, I did it by increasing powers of 2 :-) Either way, the point is it's order log2(n)-ish,

                            K 1 Reply Last reply 23 Nov 2019, 10:14
                            0
                            • J JonB
                              23 Nov 2019, 10:12

                              @kshegunov
                              When I wrote this for a realloc() replacement/interface many years ago, I did it by increasing powers of 2 :-) Either way, the point is it's order log2(n)-ish,

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 23 Nov 2019, 10:14 last edited by
                              #36

                              You do know I'm old, so you mustn't trust me too much. I wouldn't do it by increasing powers of two anyway, more like a percent of the current size, otherwise you're overshooting the needed capacity most of the time.

                              Read and abide by the Qt Code of Conduct

                              J 1 Reply Last reply 23 Nov 2019, 13:03
                              0
                              • K kshegunov
                                23 Nov 2019, 10:14

                                You do know I'm old, so you mustn't trust me too much. I wouldn't do it by increasing powers of two anyway, more like a percent of the current size, otherwise you're overshooting the needed capacity most of the time.

                                J Offline
                                J Offline
                                JonB
                                wrote on 23 Nov 2019, 13:03 last edited by JonB
                                #37

                                @kshegunov
                                You wrote:

                                and after that it [Qt] doubles its size on each realloc.

                                I wrote

                                I did it by increasing powers of 2

                                Doesn't "doubling" mean "the next power of 2"? That's what I was trying to convey.

                                2^n < x < 2^(n +1)
                                =>
                                2^(n + 1) < 2*x < 2^(n + 2)
                                

                                Right?

                                K 1 Reply Last reply 24 Nov 2019, 17:22
                                0
                                • J JonB
                                  23 Nov 2019, 13:03

                                  @kshegunov
                                  You wrote:

                                  and after that it [Qt] doubles its size on each realloc.

                                  I wrote

                                  I did it by increasing powers of 2

                                  Doesn't "doubling" mean "the next power of 2"? That's what I was trying to convey.

                                  2^n < x < 2^(n +1)
                                  =>
                                  2^(n + 1) < 2*x < 2^(n + 2)
                                  

                                  Right?

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 24 Nov 2019, 17:22 last edited by
                                  #38

                                  @JonB said in Simple big-looped program break down:

                                  Doesn't "doubling" mean "the next power of 2"?

                                  Yeah. I'm not at the top of my game, so you can probably safely ignore me.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  1

                                  32/38

                                  21 Nov 2019, 13:52

                                  • Login

                                  • Login or register to search.
                                  32 out of 38
                                  • First post
                                    32/38
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved