Results 1 to 14 of 14

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.