What influence (time) can a single if statement have in a closed loop?

I am working on a C ++ application that uses a tight loop to repeat states in FSM . Right now, due to the tight cycle, it uses a 100% processor, and our customers don’t like it. I wanted to try to put sleep(1) in a closed loop in order to weaken it, but we are worried that this will make it sleep too long between states for our large clients (whose states change very quickly!). I thought to try something like this:

 if(smallcustomer) { sleep(1); } 

And a smallcustomer will be determined somewhere else when the program starts. Does this β€œif” statement simply slow down until there is a dream and its own goal conquers?

+4
source share
8 answers

Your opinion is that FSM does not really need 100% CPU, which makes me assume that you spend a lot of time doing nothing other than checking if you need to go to the next state or not. You say that you are worried about sleeping β€œtoo long” for larger clients, which means that you are worried that some event will be missed: queue filling, mouse click, received packet, disk I / O completion clicked anything. You must reorganize to trigger this event (or events) asynchronously instead of forcing the CPU to do nothing in the loop.

+7
source

Short answer: I suspect that a simple if () statement will not hurt. However, the golden rule of optimization is test testing.

Longer answer: A slightly simpler (albeit more complicated) approach is to move the laborious FSM processing to a separate / background thread, possibly with a lower scheduling priority. This can give you the best of both worlds: faster processing when the processor is free, and less system wear due to lower priority.

Only my 2 cents ...

+2
source

A simple if statement should compile the comparison and branch instruction (on some platforms you will get it, and on others you will get two), which will run very quickly.

Instead of getting your application to sleep, see if your platform supports multi-tasking (so your process can lead to execution) or a way to lower the planning priority for your process.

If the platform supports it, usleep will give you more accurate grain management than sleep .

+1
source

Assuming your FSM works as designed, it should be possible to explain to affected customers that idle processors do not indicate a well-designed program. Any code that never enters a standby state will use a 100% processor during its time cut. In many cases, this will be a point of sale.

What you do is decrease the performance of your application. Are you sure you want to?

If the FSM is overly looping or constantly starving other CPU programs, this is another discussion. Profiling the application to enable verification and modification of the target code can help in this case.

+1
source

You should use a more specific synchronization tool than just sleep () - for example, the expected timers in Win32, although I have no equivalent for Linux / Mac. Sleep () style functions are quite famous for being unreliable. What would be better if you could let the client change the timer period.

0
source

A modern processor uses a rather complex chain of conditions to guess which instruction will follow a conditional branch. Because the CPU decodes and processes each instruction in parallel with many other instructions, the cost of getting a misunderstanding can be disastrous. Just reordering the tests in the branch, or even code that comes immediately before or after, can lead to a change in the forecast. Unfortunately, there is no easy way to predict what will work better.

For this reason, the only way to make informed decisions about optimizing the processor binding code (as you describe) is to measure what the code really does, make small changes, and measure again to see if there are any improvements.

If you really use a soft real-time application and it uses a 100% processor, this probably does not mean that you should try to scale the usage back, but provide more processor to use it, since the input exceeds the application's ability to maintain speed. In fact, scaling or output is probably cheaper than improving code performance; server hardware is cheap compared to development time.

0
source

If you're really concerned about conventions, try this suggestion for predicting branching in GCC:

 #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) if(likely(smallcustomer)) { sleep(1); } 
0
source

Your design is almost certainly wrong. Busy expectations are justified only in real-time applications. Even then, if your controller performs several parallel operations, you can implement it with interruptions, if possible.

Whatever your mind, you can move the if out of the loop. You can use patterns to prevent copying pasted code:

 template<bool flag> void mysleep(){} template<> void mysleep<true>() { sleep(1); } template<bool flag> void myf() { for(;;) { // your loop goes here mysleep<flag>(); } } void f() { smallcustomer ? myf<true>() : myf<false>(); } 

Now call f(); .

0
source

All Articles