+ Reply to Thread
Results 1 to 14 of 14

Thread: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem?

  1. #1
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem?

    Hello,

    Playing with the 'auto' keyword.
    I am trying to do a test on speed with vectors against arrays.
    As you can see using a vector works fine, using an array does'nt.
    Arrays always gives a time of zero.
    Can some one please point out my error?

    Thanks

    Qt Code:
    1. auto v = {10, 1, 2, 3, 4, 5};
    2.  
    3. auto start_time3 = std::chrono::high_resolution_clock::now();
    4. for (int s = 0; s < 250000; s++)
    5. f3 (2147483647, v);
    6. auto end_time3 = std::chrono::high_resolution_clock::now();
    7.  
    8. auto time3 = end_time3 - start_time3;
    9. std::cout << "f3 took " << std::chrono::duration_cast<std::chrono::nanoseconds>(time3).count()
    10.  
    11. //f3 took 46879000 to run.
    12.  
    13. void MainWindow::f3(int n, std::vector<int> v) {
    14. int k;
    15. for (int i = 0; i < n; i++)
    16. {
    17. for (int i = 0; i <= v.size(); i++)
    18. {
    19. //k = v.at(i); //ok both of these are similar times
    20. k = v[i]; //ok
    21. //qDebug() << k << i;
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. int a[] = {10, 1, 2, 3, 4, 5};
    2.  
    3. auto start_time4 = std::chrono::high_resolution_clock::now();
    4. for (int s = 0; s < 250000; s++)
    5. f4 (2147483647, a, (sizeof(a)/sizeof(a[0])));
    6. auto end_time4 = std::chrono::high_resolution_clock::now();
    7.  
    8. auto time4 = end_time4 - start_time4;
    9. std::cout << "f4 took " << std::chrono::duration_cast<std::chrono::nanoseconds>(time4).count()
    10.  
    11. //f4 took 0 to run.
    12.  
    13. void MainWindow::f4(int n, int arr[], int size) {
    14. int k;
    15. for (int i = 0; i < n; i++)
    16. {
    17. for (int i = 0; i <= size - 1; i++)
    18. {
    19. k = arr[i]; //ok
    20. //qDebug() << k << i;
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jimbo; 4th January 2017 at 13:05.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,068
    Thanks
    35
    Thanked 1,462 Times in 1,416 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem?

    Does it also give you "0" when you do the qDebug in side the loop?
    And the qDebug() output is printed every time?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem?

    Hello anda_skoa,

    Thanks for your reply.
    The debug output prints every time.
    I changed some of the loop counts and it ran as expected.
    I guess one/some of the 'chrono' internal counters was/were over-flowing.
    I'll have a play with other timer periods.

    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,068
    Thanks
    35
    Thanked 1,462 Times in 1,416 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem?

    Quote Originally Posted by jimbo View Post
    I changed some of the loop counts and it ran as expected.
    I guess one/some of the 'chrono' internal counters was/were over-flowing.
    Ah, good.

    One thing I always check when such functions apparently don't need any time to execute is that they have some result or side effect, e.g. like the qDebug().
    Otherwise one can't be sure that the compiler isn't detecting that it does nothing and just optimizes it away.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem?

    Hello,

    I've found a way to turn off optimisation for a function.
    This seems to work, the time output shows.

    Qt Code:
    1. #pragma GCC push_options
    2. #pragma GCC optimize ("O0") //capital 0 - zero
    3. void MainWindow::f4(int n, int arr[], int size) {
    4. int k;
    5. for (int j = 0; j < n; j++)
    6. {
    7. for (int i = 0; i <= size - 1; i++)
    8. {
    9. k = arr[i]; //ok
    10. //qDebug() << k << i << j;
    11. }
    12. }
    13. }
    14. #pragma GCC pop_options
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. #6
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Hello,

    Some simple speed tests.

    Regards
    Qt Code:
    1. void f0(int n, std::vector<int> v);
    2. void f1(int n, std::vector<int> v);
    3. void f2(int n, std::vector<int> v);
    4. void f3(int n, std::vector<int> v);
    5. void f4(int n, int arr[], int size);
    6. void f5(int n, int arr[], int size);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //**********
    2. double d;
    3. //auto v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    4. std::vector<int> v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    5. std::cout << "vector\n";
    6. //**********
    7. auto start_time = std::chrono::high_resolution_clock::now();
    8. //for (int t = 0; t < 100; t++)
    9. for (auto t = 0; t != 100; ++t)
    10. {
    11. for (int s = 0; s < 1000; s++)
    12. f0 (1000, v);
    13. }
    14. auto end_time = std::chrono::high_resolution_clock::now();
    15. auto time = end_time - start_time;
    16. d = std::chrono::duration_cast<std::chrono::microseconds>(time).count();
    17. std::cout << "f0 took " << (d / 100) << " microseconds.\n";
    18. //**********
    19. auto start_time6 = std::chrono::high_resolution_clock::now();
    20. for (int t = 0; t < 100; t++)
    21. //for (auto t = 0; t != 100; ++t)
    22. {
    23. for (int s = 0; s < 1000; s++)
    24. f0 (1000, v);
    25. }
    26. auto end_time6 = std::chrono::high_resolution_clock::now();
    27. auto time6 = end_time6 - start_time6;
    28. d = std::chrono::duration_cast<std::chrono::microseconds>(time6).count();
    29. std::cout << "f6 took " << (d / 100) << " microseconds.\n";
    30. //**********
    31. auto start_time1 = std::chrono::high_resolution_clock::now();
    32. for (int t = 0; t < 100; t++)
    33. {
    34. for (int s = 0; s < 1000; s++)
    35. f1 (1000, v);
    36. }
    37. auto end_time1 = std::chrono::high_resolution_clock::now();
    38. auto time1 = end_time1 - start_time1;
    39. d = std::chrono::duration_cast<std::chrono::microseconds>(time1).count();
    40. std::cout << "f1 took " << (d / 100) << " microseconds.\n";
    41. //**********
    42. auto start_time2 = std::chrono::high_resolution_clock::now();
    43. for (int t = 0; t < 100; t++)
    44. {
    45. for (int s = 0; s < 1000; s++)
    46. f2 (1000, v);
    47. }
    48. auto end_time2 = std::chrono::high_resolution_clock::now();
    49. auto time2 = end_time2 - start_time2;
    50. d = std::chrono::duration_cast<std::chrono::microseconds>(time2).count();
    51. std::cout << "f2 took " << (d / 100) << " microseconds reverse.\n";
    52. //**********
    53. auto start_time3 = std::chrono::high_resolution_clock::now();
    54. for (int t = 0; t < 100; t++)
    55. {
    56. for (int s = 0; s < 1000; s++)
    57. f3 (1000, v);
    58. }
    59. auto end_time3 = std::chrono::high_resolution_clock::now();
    60. auto time3 = end_time3 - start_time3;
    61. d = std::chrono::duration_cast<std::chrono::microseconds>(time3).count();
    62. std::cout << "f3 took " << (d / 100)
    63. << " microseconds.\n";
    64. //**********
    65. int a[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    66. std::cout << "\narray\n";
    67. //**********
    68. auto start_time4 = std::chrono::high_resolution_clock::now();
    69. for (int t = 0; t < 100; t++)
    70. {
    71. for (int s = 0; s < 1000; s++)
    72. f4 (1000, a, (sizeof(a)/sizeof(a[0])));
    73. }
    74. auto end_time4 = std::chrono::high_resolution_clock::now();
    75. auto time4 = end_time4 - start_time4;
    76. d = std::chrono::duration_cast<std::chrono::microseconds>(time4).count();
    77. std::cout << "f4 took "
    78. << (d / 100)
    79. << " microseconds.\n";
    80. //**********
    81. auto start_time5 = std::chrono::high_resolution_clock::now();
    82. for (int t = 0; t < 100; t++)
    83. {
    84. for (int s = 0; s < 1000; s++)
    85. f5 (1000, a, (sizeof(a)/sizeof(a[0])));
    86. }
    87. auto end_time5 = std::chrono::high_resolution_clock::now();
    88. auto time5 = end_time5 - start_time5;
    89. d = std::chrono::duration_cast<std::chrono::microseconds>(time5).count();
    90. std::cout << "f5 took "
    91. << (d / 100)
    92. << " microseconds reverse.\n";
    93. //**********
    94. std::cout << "\n";
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #pragma GCC push_options
    2. #pragma GCC optimize ("O0")
    3. void MainWindow::f0(int n, std::vector<int> v) {
    4. int k;
    5. for (int i = 0; i < n; i++)
    6. {
    7. for (const int& i : v) // access by const reference
    8. {
    9. k = i;
    10. }
    11. }
    12. }
    13. #pragma GCC pop_options
    14.  
    15. #pragma GCC push_options
    16. #pragma GCC optimize ("O0")
    17. void MainWindow::f1(int n, std::vector<int> v) {
    18. int k;
    19. for (int i = 0; i < n; i++)
    20. {
    21. for (auto i : v) // access by value, the type of i is int
    22. {
    23. k = i;
    24. }
    25. }
    26. }
    27. #pragma GCC pop_options
    28.  
    29. #pragma GCC push_options
    30. #pragma GCC optimize ("O0")
    31. void MainWindow::f2(int n, std::vector<int> v) {
    32. int k;
    33. for (int i = 0; i < n; i++)
    34. {
    35. for (auto i = v.end() - 1; i >= v.begin(); --i) //reverse
    36. {
    37. k = *i;
    38. //qDebug() << k << *i;
    39. }
    40. }
    41. }
    42. #pragma GCC pop_options
    43.  
    44. #pragma GCC push_options
    45. #pragma GCC optimize ("O0")
    46. void MainWindow::f3(int n, std::vector<int> v) {
    47. int k;
    48. for (int i = 0; i < n; i++)
    49. {
    50. for (int i = 0; i <= v.size(); i++) //conventional style
    51. {
    52. //k = v.at(i); //ok both of these are similar times
    53. k = v[i]; //ok
    54. //qDebug() << k << i;
    55. }
    56. }
    57. }
    58. #pragma GCC pop_options
    59.  
    60. #pragma GCC push_options
    61. #pragma GCC optimize ("O0")
    62. void MainWindow::f4(int n, int arr[], int size) {
    63. int k;
    64. for (int i = 0; i < n; i++)
    65. {
    66. for (int i = 0; i <= size - 1; i++)
    67. {
    68. k = arr[i]; //ok
    69. //qDebug() << k << i << j;
    70. }
    71. }
    72. }
    73. #pragma GCC pop_options
    74.  
    75. #pragma GCC push_options
    76. #pragma GCC optimize ("O0")
    77. void MainWindow::f5(int n, int arr[], int size) {
    78. int k;
    79. for (auto i = 0; i != n; ++i)
    80. {
    81. for (auto i = size - 1; i >= 0; --i) //reverse
    82. {
    83. k = arr[i]; //ok
    84. //qDebug() << arr[i];
    85. }
    86. }
    87. }
    88. #pragma GCC pop_options
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. vector
    2. f0 took 171214 microseconds.
    3. f6 took 171078 microseconds.
    4. f1 took 147593 microseconds.
    5. f2 took 154707 microseconds reverse.
    6. f3 took 87834.4 microseconds.
    7.  
    8. array
    9. f4 took 71596.9 microseconds.
    10. f5 took 66486.2 microseconds reverse.
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,448
    Thanks
    203
    Thanked 535 Times in 525 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Interesting. You should also try this using std:: vector<int>:: const_iterator() and std:: vector<int>:: iterator() (added whitespace to avoid the "smiley effect"). In our testing, iterator-based access was up to an order of magnitude slower than pointer or operator[] based access in MSVC++.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Hello d_stranz,

    I don't quite understand the use of 'const'.
    However I ran test 'f7' along with those already posted.

    Regards
    Qt Code:
    1. auto start_time7 = std::chrono::high_resolution_clock::now();
    2. for (int t = 0; t < 100; t++)
    3. {
    4. for (int s = 0; s < 1000; s++)
    5. f7 (1000, v);
    6. }
    7. auto end_time7 = std::chrono::high_resolution_clock::now();
    8. auto time7 = end_time7 - start_time7;
    9. d = std::chrono::duration_cast<std::chrono::microseconds>(time7).count();
    10. std::cout << "f7 took " << (d / 100) << " microseconds.\n";
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #pragma GCC push_options
    2. #pragma GCC optimize ("O0")
    3. void MainWindow::f7(int n, std::vector<int> v) {
    4. int k;
    5. for (int i = 0; i < n; i++)
    6. {
    7. for (std::vector<int>::iterator it = v.begin() ; it != v.end(); ++it)
    8. {
    9. k = *it;
    10. //qDebug() << *it << k;
    11. }
    12. }
    13. }
    14. #pragma GCC pop_options
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. vector
    2. f0 took 168825 microseconds.
    3. f6 took 167124 microseconds.
    4. f1 took 136416 microseconds.
    5. f2 took 155180 microseconds reverse.
    6. f3 took 89297.5 microseconds.
    7. f7 took 128919 microseconds. //***
    8.  
    9. array
    10. f4 took 71238 microseconds.
    11. f5 took 66366.4 microseconds reverse.
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,448
    Thanks
    203
    Thanked 535 Times in 525 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Even more interesting. The Microsoft C++ implementation of STL iterators is definitely slower than the GCC version.

    The const_iterator returns a const reference to the value stored in the vector. This guarantees to the compiler that the caller will not modify the value and thus might allow it to generate more optimized (i.e. faster) code than using the non-const iterator.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #10
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Hello d_stranz,

    OS - Windows 10 - 32 bit

    g++ -v
    gcc version 5.3.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)

    Regards

  11. The following user says thank you to jimbo for this useful post:

    d_stranz (11th January 2017)

  12. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,448
    Thanks
    203
    Thanked 535 Times in 525 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    When I get some free time I will run your tests using MSVC++ in Visual Studio 2013 / Windows 10 x64 and post my findings. Good work here.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  13. #12
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Hello,

    OS - Windows 7 - 32 Bit
    Same machine - dual boot system

    g++ -v
    gcc version 5.3.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)


    vector
    f0 took 150927 microseconds.
    f6 took 158602 microseconds.
    f1 took 135515 microseconds.
    f2 took 160152 microseconds reverse.
    f3 took 85401.3 microseconds.
    f7 took 137652 microseconds.

    array
    f4 took 70551.1 microseconds.
    f5 took 65513.5 microseconds reverse.

    Regards

  14. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,448
    Thanks
    203
    Thanked 535 Times in 525 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Can you please make a ZIP file of your current code and attach it to a reply? There is code scattered in various places in this post, and I would like to be sure to use the same thing you are using.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  15. #14
    Join Date
    Oct 2006
    Posts
    95
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5.7/C++ - Playing with 'auto' keyword and chrono - loop problem? - speed tests

    Hello,

    Enclosed 'Tests.txt'

    Regards
    Attached Files Attached Files

+ Reply to Thread

Similar Threads

  1. Chrono
    By qtoptus in forum Qt Programming
    Replies: 12
    Last Post: 22nd December 2011, 19:15
  2. Qt emit keyword duplicate
    By mreyfout in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2010, 06:10
  3. Inline keyword doubt and... exist outline keyword ?
    By tonnot in forum General Programming
    Replies: 4
    Last Post: 24th September 2010, 00:51
  4. Problem with playing color-keyed AxWidget (Shockwave Flash)
    By indiocolifa in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2009, 21:40
  5. QSound problem playing different wav files
    By couker in forum Qt Programming
    Replies: 6
    Last Post: 16th September 2009, 21:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts