Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++ std::chrono really slow in Qt
Forum Updated to NodeBB v4.3 + New Features

C++ std::chrono really slow in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.3k Views 3 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.
  • P Offline
    P Offline
    parysudo
    wrote on 20 May 2023, 16:08 last edited by parysudo
    #1

    Hello everyone this is my first post.

    I write because I try to make a real time simulation in Qt C++ and I have the following issue:

    I am using the code below to see in console the real time counting up. From Visual Code Studio it works fine and smooth, but in Qt it is really slow (takes several minutes per second)...

    #include <iostream>
    #include <chrono>
    
    int main(int argc, char *argv[])
    {
        auto startTime = std::chrono::system_clock::now();
        while (true) {
            auto currentTime = std::chrono::system_clock::now();
            std::chrono::duration<double> elapsedTime = currentTime - startTime;
    
        std::cout << elapsedTime.count() << std::endl;
    
        }
        return 0;
    }
    

    The code is the same, what can be the problem?

    Thanks

    J C 2 Replies Last reply 20 May 2023, 16:31
    0
    • P parysudo
      20 May 2023, 16:08

      Hello everyone this is my first post.

      I write because I try to make a real time simulation in Qt C++ and I have the following issue:

      I am using the code below to see in console the real time counting up. From Visual Code Studio it works fine and smooth, but in Qt it is really slow (takes several minutes per second)...

      #include <iostream>
      #include <chrono>
      
      int main(int argc, char *argv[])
      {
          auto startTime = std::chrono::system_clock::now();
          while (true) {
              auto currentTime = std::chrono::system_clock::now();
              std::chrono::duration<double> elapsedTime = currentTime - startTime;
      
          std::cout << elapsedTime.count() << std::endl;
      
          }
          return 0;
      }
      

      The code is the same, what can be the problem?

      Thanks

      J Online
      J Online
      JonB
      wrote on 20 May 2023, 16:31 last edited by
      #2

      @parysudo said in C++ std::chrono really slow in Qt:

      From Visual Code Studio it works fine and smooth, but in Qt it is really slow

      Visual Code Studio is an IDE. Qt is not. Nor is it a language. Nor does it have anything to do with how C++ std::... stuff works, including std::chrono.

      When run outside an IDE your code should take the same amount of time regardless of how you built it, with or without Qt (it does not use any Qt calls).

      So do you mean when running inside the Qt Creator IDE? Then it probably comes down to how long the std::cout takes in each IDE to display loads of text very fast in its "stdout output window". Which might differ from one IDE to another.

      If you comment out that line you will discover std::chrono cannot be "slow".

      This code tests nothing other than that, I suggest you look at a more realistic code if you are trying to measure something.

      P 1 Reply Last reply 20 May 2023, 16:34
      3
      • P parysudo
        20 May 2023, 16:08

        Hello everyone this is my first post.

        I write because I try to make a real time simulation in Qt C++ and I have the following issue:

        I am using the code below to see in console the real time counting up. From Visual Code Studio it works fine and smooth, but in Qt it is really slow (takes several minutes per second)...

        #include <iostream>
        #include <chrono>
        
        int main(int argc, char *argv[])
        {
            auto startTime = std::chrono::system_clock::now();
            while (true) {
                auto currentTime = std::chrono::system_clock::now();
                std::chrono::duration<double> elapsedTime = currentTime - startTime;
        
            std::cout << elapsedTime.count() << std::endl;
        
            }
            return 0;
        }
        

        The code is the same, what can be the problem?

        Thanks

        C Online
        C Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on 20 May 2023, 16:32 last edited by
        #3

        Hi, welcome to the forum.

        Qt is a library and you're not using it here. Do you mean Qt Creator (the IDE)?

        What you're doing here is basically stress testing the standard stream throughput of the thing that redirects it. It has nothing to do with chrono. You might as well just do

        while(true)
            std::cout << "Hello" << std::endl;
        

        cout is a stream that gets redirected by the IDE (either VS Code or Creator) to its window. It's buffered and issuing std::endl flushes that buffer i.e. it's a blocking operation that waits until the receiver gets (and potentially processes) all the data in the stream.

        what can be the problem?

        There is no problem. Pumping megabytes of formatted text non-stop through standard stream is not something it is designed for. What you're seeing is just how fast a given IDE is able to process that amount of stream data.

        1 Reply Last reply
        2
        • J JonB
          20 May 2023, 16:31

          @parysudo said in C++ std::chrono really slow in Qt:

          From Visual Code Studio it works fine and smooth, but in Qt it is really slow

          Visual Code Studio is an IDE. Qt is not. Nor is it a language. Nor does it have anything to do with how C++ std::... stuff works, including std::chrono.

          When run outside an IDE your code should take the same amount of time regardless of how you built it, with or without Qt (it does not use any Qt calls).

          So do you mean when running inside the Qt Creator IDE? Then it probably comes down to how long the std::cout takes in each IDE to display loads of text very fast in its "stdout output window". Which might differ from one IDE to another.

          If you comment out that line you will discover std::chrono cannot be "slow".

          This code tests nothing other than that, I suggest you look at a more realistic code if you are trying to measure something.

          P Offline
          P Offline
          parysudo
          wrote on 20 May 2023, 16:34 last edited by
          #4

          @JonB Yeah my apologies I mean Qt Creator. And I will try to test it with no cout to see. Thanks!

          C 1 Reply Last reply 20 May 2023, 16:45
          0
          • P parysudo has marked this topic as solved on 20 May 2023, 16:38
          • P parysudo
            20 May 2023, 16:34

            @JonB Yeah my apologies I mean Qt Creator. And I will try to test it with no cout to see. Thanks!

            C Online
            C Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on 20 May 2023, 16:45 last edited by
            #5

            @parysudo By the way - when testing any sort of performance or timing don't put I/O operations in the thing you test i.e. reading/writing a file, networking or streams. It makes your test useless because you're not testing your code but the thing on the other side of the I/O pipe.

            You can accumulate results in a buffer and then output them when the test is over. For example:

            std::array<std::chrono::duration<double>, SOME_SIZE> results;
            
            for (auto& result : results)
            {
               result = std::chrono::system_clock::now() - startTime;
            }
            
            for (auto result : results)
               std::cout << result.count() << "\n"; // Use "\n" not to flush every line separately
            
            std::cout << std::flush; // Flush everything at once
            
            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kent-Dorfman
              wrote on 20 May 2023, 16:51 last edited by
              #6

              To address the 2ton white elephant in the room:

              There is no such thing as a real-time simulation on any desktop PC!

              You are using a multi-tasking, preemptive (timesharing) system where background tasks can/will skew your results.

              Correct simulations emulate real-time by keeping an internal pseudo-timer counter such that the "state" of the simulation can be restored to any point by running the sim until the pseudo-timer reaches the desired value.

              You can sync your pseudo-timer to the RTC on the PC, but you need to understand that you cannot rely upon the RTC to timekeep for any real simulation.

              1 Reply Last reply
              0

              4/6

              20 May 2023, 16:34

              • Login

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