Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much impact (time) can a single 'if' statement have within a tight loop?

I'm working on an application in C++ that uses a tight loop to iterate through states in our FSM. Right now, because of the tight loop, it uses 100% CPU, and our customers don't like that. I wanted to try putting a sleep(1) in the tight loop to loosen it up, but we're worried that that will make it sleep too long between states for our large customers (whose states change very quickly!). I was thinking of trying something like this:

if(smallcustomer)
{
    sleep(1);
}

And smallcustomer would be defined somewhere else when the program started up. Does that 'if' statement just slow things down as much as sleep would, and defeating its own purpose?

like image 603
Steve Goykovich Avatar asked Dec 12 '22 17:12

Steve Goykovich


2 Answers

Your implication is that the FSM shouldn't actually need 100% of the CPU, which leads me to assume you are spending a lot of time doing nothing except checking to see if you need to move to the next state or not. You say you are worried about sleeping "too long" for larger customers, which means you are concerned that some event will be missed: a queue filling, a mouse click, a packet being received, completion of disk I/O, a key being pressed, whatever. You should refactor to trigger asynchronously on that event (or events) instead of hogging the CPU doing nothing in a loop.

like image 138
Karl Bielefeldt Avatar answered Jan 18 '23 23:01

Karl Bielefeldt


Short answer: I suspect that the simple if() statement won't hurt much. However, the golden rule of optimization is Test Test Test.

Longer answer: A somewhat cleaner (though more difficult) approach would be to move the time-consuming FSM processing to a separate / background thread, perhaps with lower scheduling priority. This might give you the best of both worlds: Fast processing when CPU is free, and less hogging of the system, due to lower priority.

Just my 2 cents...

like image 29
Eric Pi Avatar answered Jan 18 '23 22:01

Eric Pi