Application takes so much CPU usage?

Discussion in 'Mac Programming' started by mkristain, Aug 29, 2011.

  1. mkristain, Aug 29, 2011
    Last edited: Aug 29, 2011

    mkristain macrumors regular

    Joined:
    Aug 18, 2011
    #1
    hi my application takes so much cpu usage ,thats why application become slower can u please tell me how can i faster the application and reduce the cpu usage.

    i am using thread also but it does not take any effect

    like:

    Code:
    
    - (IBAction)Test_btn:(id)sender
    {
    [NSThread detachNewThreadSelector:@selector(aMethod) toTarget:self withObject:nil];
    }
    
    -(void)aMethod
    {
    	NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
    	
    	[self Test_Fun];
    
    	[pool release];
    	
        [NSThread exit];
    }
    
    -(void) Test_Fun
    {
    
    for(int i=0;i<100000;1;i++)
    {
       int j=0;
      for( j=0;j<512;j++)
      {
    	s1=[s1 stringByAppendingString:[NSString stringWithFormat:@"%02X" , buffer[j]]];
      }
    
      NSRange whereFound = [s1 rangeOfString:@"FFE0"];
      if(whereFound.location == NSNotFound && whereFound.length == 0)
       {
             NSLog(@"%@ does not contain FFE0.",s1);
       } else
       {
      NSLog(@"%@ contains xyz.",s1);
      }
    
    //do something for create and write file.
    
    //and show in KIImageBrowser
    
    }
    }
    
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    The first thing I notice is a loop of 100000 iterations that seems to have no purpose, and an extra parameter. I suspect that extra parameter will exclude the increment of i also, therefore causing an infinite loop. The other thing about this loop, is that it repeats over the same buffer from the code you've supplied.

    Second is that you are converting a buffer to an NSString and then testing that NSString. I think it would much more efficient to just scan the buffer for the two precise bytes of interest.

    If you want to insist on trying to use an NSString, then you are probably better off looking into one of the initializers that can handle the bytes from a buffer, perhaps;
    Code:
    initWithBytes:length:encoding:
    This action would remove that loop of 512 iterations.

    If I'm understanding this, there shouldn't be a need for spawning a thread, because scanning 512 bytes should be very fast. Sure, if it is 100,000 buffers of 512 bytes, then maybe the thread would be advisable.

    You may want to explain what it is you are actually trying to do.
     
  3. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #3
    NSString* s = [[NSString alloc] initWithBytes:buffer length:sizeof(buffer) encoding:NSASCIIStringEncoding]; gives string that buffer contains not hex value.
     
  4. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #4
    Well, the code you have is creating 100 million+ strings (2 strings per iteration, x 512 iterations in inner loop, x 100,000 iterations in outer loop) so that is going to take some time.

    Using an NSMutableString and appendFormat might be quicker since you're just appending one byte each time, rather than creating a new string, appending it onto the existing string, then assigning it to the original pointer.

    I'm assuming garbage collection is enabled (otherwise that code would leak), but I'm not certain how well garbage collection would work in your scenario where you have 2 tight loops with a large number of iterations. Memory usage could well 'balloon' while that program is looping.

    Another thing - is that loop you quoted correct? I've not seen a C-style for loop with 4 statements like that before, so I'm not sure if that's a typo.

    Code:
    for(int i=0;i<100000;1;i++)
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Step 1: Post correct compilable code.
    Code:
    for(int i=0;i<100000;[COLOR="Red"]1;i++[/COLOR])
    
    The red-hilited code is uncompilable.


    Step 2: Describe what you're trying to accomplish.

    Guessing from the pattern you're searching for (FF E0), and the reference to images, my guess is that you're scanning a block of memory for a JFIF header. If that's correct, please confirm this is what you're trying to do.

    If you're not scanning for a JFIF header, then please explain the overall purpose of your code.

    In any case, please explain the loop of 100000, and what it is supposed to accomplish. I see no reason why it would be needed for a JFIF header scan.
     
  7. gnasher729, Aug 29, 2011
    Last edited: Aug 29, 2011

    gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    I suggest that you use the XCode debugger, set a breakpoint in the inner loop, change display to "mixed source code and assembler", and step through the assembler code for one iteration step by step.

    When you are done, reflect on your experience and look for enlightenment. Seriously. Most of the time code is just fast enough; when it isn't, you need to know what your code is actually doing behind the scenes.

    After that figure out how to do the test that you are doing to examine "buffer" without involving any strings. Some straightforward code should do the complete test in maybe two microseconds. Note that memchr will look for specific byte values in a buffer at a rate of about 10 GB per second, if you want to do it really fast. Looking at what the contents of buffer usually looks like would help in tuning for maximum speed.


    I hope not, because if that is what he tries, then the code is plain wrong. It will also recognise a block that contains 3F FE 07, for example.

    PS. mkristain, it seems that you deliberately ask the wrong questions. You make a beginner's mistake here: You have a problem, you have a vague notion that doing X could help solving the problem, and you ask how to do X. Your X in this case seems to be the totally wrong approach to solve the problem. My guess is that

    Code:
    bool foundpattern = false;
    for (size_t i = 0; i < 511; ++i) {
        if (buffer [i] == 0xff && buffer [i + 1] == 0xfe) {
            foundpattern = true; break; 
        }
    }
    
    will solve your problems quite easily.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    I wasn't suggesting it was correct. I was merely guessing what the purpose of the code might be.


    Red-hilited code should be 0xE0.

    Also, if the goal is to find the position of the FF E0 pattern, then i needs to be declared outside the loop.
     
  9. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #10
    thanks to guide me but still cpu usage are same.
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    Post your updated code and we can see if there are obvious problems. If you are using gnasher729's code from this thread or mine from the other thread and you aren't needlessly looping 10000 times, this should take a small fraction of a second.

    -Lee
     

Share This Page