Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Setting Speed of While Loop per Second

I am relatively new to C++, so I don't have a huge amount of experience. I have learned Python, and I am trying to make an improved version of a Python code I wrote in C++. However, I want it to work in real time, so I need to set the speed of a While loop. I'm sure there is an answer, but I couldn't find it. I want a comparable code to this:

rate(timeModifier * (1/dt))

This was the code I used in Python. I can set a variable dt to make calculations more precise, and timeModifier to double or triple the speed (1 sets it to realtime). This means that the program will go through the loop 1/dt times per second. I understand I can include time.h at the header, but I guess I am too new to C++ to understand how to transfer this to my needs.

like image 643
Asaaj Avatar asked May 18 '12 13:05

Asaaj


People also ask

How does a while loop work in C++?

The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop. Here, the key point to note is that a while loop might not execute at all.

How to make a loop run faster with less time?

Set the loop iterations to 10,000. Find the time in milliseconds>Run Loop>find time in milliseconds and subtract the first timer. Do it for both codes, what ever one has the lowest milliseconds it runs faster. You might want to run the test multiple times and average them out to reduce the likelihood of background processes influencing the test.

How much slower is the for4_foreach loop?

It’s runs in 8.9 ms, about 20% slower than the loop using postfix increments (7.5 ms) with the extra time corresponding to one extra cycle per iteration of the inner loop. The interesting bit is to compare this result to the loop For4_Foreach where the inner loop has been replaced by a foreach loop:

What are the different types of looping statements in C?

Depending upon the position of a control statement in a program, looping statement in C is classified into two types: 1. Entry controlled loop 2. Exit controlled loop In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.


2 Answers

You could write your own timer class:

#include <ctime>

class Timer {
    private:
        unsigned long startTime;
    public:
        void start() {
            startTime = clock();
        }

        unsigned long elapsedTime() {
            return ((unsigned long) clock() - startTime) / CLOCKS_PER_SEC;
        }

        bool isTimeout(unsigned long seconds) {
            return seconds >= elapsedTime();
        }
};


int main() 
{
    unsigned long dt = 10; //in seconds
    Timer t;
    t.start();

    while(true) 
    {
        if(t.elapsedTime() < dt) 
        {
            //do something to pass time as a busy-wait or sleep
        }
        else 
        {
            //do something else
                    t = Timer(); //reset the timer
        }
    }
}

Note that busy-waits are discouraged, since they will hog the CPU. If you don't need to do anything, use the sleep command(Windows) or usleep ( Linux). For more information on making timers in C++, see this link.

like image 83
slybloty Avatar answered Sep 30 '22 02:09

slybloty


You can't do it the same manner in C++. You need to manually call some kind of sleep function in calculation loop, Sleep on Windows or usleep on *NIX.

like image 25
inkooboo Avatar answered Sep 30 '22 01:09

inkooboo