Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttle CPU usage on background thread

I have a CPU intensive task and I want it to use less CPU and take up more time.

I'm loading a massive amount of SCNNodes to a scene at start-up. It takes up a lot of memory, and I'd like it to work on it at a safe rate instead of lagging up my system or potentially crashing it.

Here's the code I'm using to load the nodes.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
    NSLog(@"Start Loading Level");
    SCNNode *cameraNode = [SCNNode node];
    cameraNode.camera = [SCNCamera camera];
    cameraNode.camera.zFar = 5000;
    cameraNode.position = SCNVector3Make(0, 3000, 0);
    cameraNode.rotation = SCNVector4Make(1, 0, 0, -M_PI_2);
    [scene.rootNode addChildNode:cameraNode];
    for (int i = 0; i < 100000; i++)
    {
        int side = 3000;
        SCNNode *node = [SCNNode node];
        node.geometry = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0];
        node.position = SCNVector3Make(arc4random_uniform(side) - side / 2.0,
                                       0,
                                       arc4random_uniform(side) - side / 2.0);
        [scene.rootNode addChildNode:node];
    }
    dispatch_async(dispatch_get_main_queue(), ^(void){
        NSLog(@"Finished");
    });
});

Here are the statistics:

Image Usage over TimeImage Memory

like image 296
Konrad Wright Avatar asked Apr 17 '15 17:04

Konrad Wright


People also ask

How do I reduce CPU thread usage?

The best solution to limiting the cpu usage for a process or thread is to make sure that the thread or process uses less cpu. That can best be done by improving the efficiency of the code, or by calling it less often. The aim is to make sure that the process doesn't continually consume all of its available time slice.

Why is my CPU usage at 100%?

If the CPU usage is around 100%, this means that your computer is trying to do more work than it has the capacity for. This is usually OK, but it means that programs may slow down a little. Computers tend to use close to 100% of the CPU when they are doing computationally-intensive things like running games.


1 Answers

It might not be the best way to do this, but have you looked at prepareobject:?

Creating the NSObjects themselves shouldn't be that much of a problem, but preparing the geometry and adding it to the scene probably is. Bu deffering this step to a secondary thread you'll save some CPU and make the scene not stop until the node is ready to be added.

Once each node is ready, (completion handler) just add it to the scene. I won't throttle the loading, but it will at least handle it in a safer way for the rest of your app.

https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/SCNSceneRenderer_Protocol/index.html#//apple_ref/occ/intfm/SCNSceneRenderer/prepareObjects:withCompletionHandler:

-(void)prepareLevel{
    // Mutable Array to save the nodes
    NSMutableArray *nodes = [[NSMutableArray alloc] init];

    for (int i = 0; i < 100000; i++)
    {
        int side = 3000;
        SCNNode *node = [SCNNode node];
        node.geometry = [SCNBox boxWithWidth:1 height:1 
                                      length:1 chamferRadius:0];
        node.position = SCNVector3Make(arc4random_uniform(side) - side / 2.0,
                                       0,
                                       arc4random_uniform(side) - side / 2.0);

        [nodes addObject:node];
    }

    [mySCNView prepareObjects:nodes withCompletionHandler:^(BOOL success) {
        for(SCNNode*node in nodes){
            [scene.rootNode addChildNode:node];
        }
    }];

}
like image 165
Moustach Avatar answered Oct 12 '22 15:10

Moustach