slower loop 2nd time

This is a discussion on slower loop 2nd time within the C++ Programming forums, part of the General Programming Boards category; i have a function that has 4 For loops in it and it does a certain task and uses 100% ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    67

    Question slower loop 2nd time

    i have a function that has 4 For loops in it and it does a certain task and uses 100% cpu, it also moves the mouse around. at the end it asks the user if the user wants to repeat the function. if the user says yes then the function runs again. the first time the function runs i can see the mouse move pretty quickly in the pattern it is suppose to move in. the 2nd time it goes about twice as slow. does any1 know y? i don't think posting the code is neccessary since i spelled it out pretty much and there's a lot of junk in the loops which would take a while to take out.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Take out the junk anyway...

    Umm... any possible memory leaks? Watch the memory requirement of the app during the loops.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    Code:
    for (INT col = cntx; col < (cntx + 160); col += 10)
    {
    	for (INT row = cnty; row < (cnty + 160); row += 10)
    	{
    		for (INT y = (row - 20); y < (row + 30); y += 10)
    		{
    			for (INT x = (col - 20); x < (col + 30); x += 10)
    			{
    				// Junk removed. Pretty much compared pixel colors
    			}	
    		}
    		SetCursorPos(col,row);
    	}	
    }
    cout << "Press 'e' to exit or 'Enter' to continue'" << endl;
    cin.getline(check, 10, '\n');
    if(!strcmpi("e", check))
    {
    	cout << "Exiting" << endl;
    	return 0;
    }
    else
    {
    	cout << "Continueing" << endl;
                    // Junk removed. Also returns back to beginning of master loop or function...either way same effect.
    }
    Last edited by Salem; 07-27-2004 at 04:22 PM. Reason: TAGGED!!!!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >// Junk removed. Also returns back to beginning of master loop or function...either way same effect.

    How does it do this (return to beginning of master loop). Is it a do-while loop? Do you use a goto statement? Or some other method?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    a while loop


    while (true)
    {
    // All the above junk
    }



    so i figure it will keep running until the user doesn't want it to. in which it will return 0 and exit the program.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well nothing pops out at me, but I could be missing something.

    This doens't address your original problem, but you could try speeding up your code by evaluating the end conditions prior to the start of the loop, i.e.
    Code:
    end_col = cntx + 160;
    for (INT col = cntx; col < end_col; col += 10)
    The compiler may be smart enough to optimise this, so you may not see any difference.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    i kept a lot of the code together and took out some stuff you didn't need in there. this is a working example. you should see the mouse move for about 11-15 seconds depending on the computer. if you press enter twice after it asks you to then it will take 25-35 seconds.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h> 
    #include <windows.h>
    #include <cstdio> 
    #include <conio.h>
    #pragma comment(lib, "gdi32.lib")
    
    int main()
    {
    	COLORREF clr;
    	HDC      hdcScrn;
    	POINT    pt;
    	INT cntx = 100;
    	INT cnty = 100;
    	CHAR check[10];
    
    	while (true)
    	{
    		for (INT col = cntx; col < 260; col += 10)
    		{
    			for (INT row = cnty; row < 260; row += 10)
    			{
    				for (INT y = (row - 20); y < (row + 30); y += 10)
    				{
    					for (INT x = (col - 20); x < (col + 30); x += 10)
    					{
    						GetCursorPos(&pt);
    						hdcScrn=CreateDC("DISPLAY",0,0,0);
    						clr=GetPixel(hdcScrn,pt.x,pt.y);
    						COLORREF pixColor = GetPixel(hdcScrn,x,y);
    						SetCursorPos(col,row);
    						// Some junk removed.
    					}	
    				}
    			}
    		}
    		cout << "Press 'e' to exit or 'Enter' to continue'" << endl;
    		cin.getline(check, 10, '\n');
    		if(!strcmpi("e", check))
    		{
    			cout << "Exiting" << endl;
    			return 0;
    		}
    		else
    		{
    			cout << "Continueing" << endl;
    		}
    		DeleteDC(hdcScrn);
    		getchar();
    	}
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > hdcScrn=CreateDC("DISPLAY",0,0,0);
    There's your problem. You are creating thousands of new DC's without ever deleting them. You probably just need to create one each time you loop back.
    Code:
    		hdcScrn=CreateDC("DISPLAY",0,0,0);
    		for (INT col = cntx; col < 260; col += 10)
    		{
    			for (INT row = cnty; row < 260; row += 10)
    			{
    				for (INT y = (row - 20); y < (row + 30); y += 10)
    				{
    					for (INT x = (col - 20); x < (col + 30); x += 10)
    					{
    						GetCursorPos(&pt);
    						clr=GetPixel(hdcScrn,pt.x,pt.y);
    						COLORREF pixColor = GetPixel(hdcScrn,x,y);
    						SetCursorPos(col,row);
    						// Some junk removed.
    					}	
    				}
    			}
    		}
    Or otherwise delete the DC within the inner loop.
    Code:
    						GetCursorPos(&pt);
    						hdcScrn=CreateDC("DISPLAY",0,0,0);
    						clr=GetPixel(hdcScrn,pt.x,pt.y);
    						COLORREF pixColor = GetPixel(hdcScrn,x,y);
    						SetCursorPos(col,row);
    						// Some junk removed.
    						DeleteDC(hdcScrn);
    Last edited by swoopy; 07-28-2004 at 12:33 PM.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    thank you very much


    so was that considered a memory leak?..or just plain clumsiness with loops

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >so was that considered a memory leak?..or just plain clumsiness with loops

    A memory leak. Handles in windows are indirect pointers to some memory. So when you do a CreateDC() it allocates memory. I think any time you create some object in windows, it will allocate memory (for example, a DC, bitmap, brush, pen).

    The gurus on the Windows board could explain this better ... Novacain, Fordy, and Ken F.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  2. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 11:29 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21