Thread: Best way to break out of loop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Ducky View Post
    Thats pretty fast indeed but it makes your CPU works 100%. (50% if you got a dual core)

    Now put a Sleep(1); in the loop to avoid this and the performance is gonna drop down significantly.

    A 1000 tries take 2 secs.
    Well when you're repeatedly telling it to sleep for 1ms, that's going to add up to a lot of time sleeping if you don't do much else. In fact the sleeping alone there has to account for at least 1 second according to how Sleep is documented, and typically more. The fact that your real code will actually be doing something useful will mean that overall it wont be spending quite as much time sleeping.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks for the comments!

    @iMalc "The fact that your real code will actually be doing something useful will mean that overall it wont be spending quite as much time sleeping."

    Yes i didnt realize that, my bad.

    @BMJ



    Code:
    void ShakeIt() 
    {
        RECT rcWndSz;
        int iX = 0;
        int iY = 0;
        int iScreenX = GetSystemMetrics(SM_CXSCREEN);
        int iScreenY = GetSystemMetrics(SM_CYSCREEN);
        
        DWORD oldtick = GetTickCount();
        while (1)
        {
            if (GetWindowRect(hWnd, &rcWndSz))
            {
                iX = rcWndSz.left + ((rand() % 5) * ((rand() % 100) > 50 ? 1 : -1));
                iY = rcWndSz.top  + ((rand() % 5) * ((rand() % 100) > 50 ? 1 : -1));
    
                iX = iX > iScreenX ? iScreenX : iX;
                iY = iY > iScreenY ? iScreenY : iY;
                iX = iX < 0 ? 0 : iX;
                iY = iY < 0 ? 0 : iY;
    
                SetWindowPos(hWnd, NULL, iX, iY, 0, 0, SWP_NOSIZE);
                Sleep(20);
            }
            
            if ((GetTickCount() - oldtick)> 3000) break;
        }
    }
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  2. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM