2

I have an array like following example:

[
    {
        "lx": 144,
        "ly": 57,
        "mx": 144,
        "my": 56
    },
    {
        "lx": 321,
        "ly": -4,
        "mx": 298,
        "my": 16
    }
]

I want to set every element with its half value.

In short, I want something like following.

[
    {
        "lx": 72,
        "ly": 28.5,
        "mx": 72,
        "my": 28
    },
    {
        "lx": 160.5,
        "ly": -2,
        "mx": 149,
        "my": 8
    }
]

I am using following code:

    NSMutableArray * tempArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < jsonArray.count; i++)
    {
        NSMutableDictionary * tempDict = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)jsonArray[i]];
        tempDict[@"lx"] = @([tempDict[@"lx"] intValue]/2);
        tempDict[@"ly"] = @([tempDict[@"ly"] intValue]/2);
        tempDict[@"mx"] = @([tempDict[@"mx"] intValue]/2);
        tempDict[@"my"] = @([tempDict[@"my"] intValue]/2);
        [tempArray addObject:tempDict];
    }

I have done it with a loop and it is working fine with small amount of data.

But when I have large data, App behaviour becomes slow.

Any help will be appreciated

Thanks...

4
  • If you want to improve its performance, you'd better post your code. Commented Aug 8, 2014 at 8:18
  • I wonder if "9/2=4" is ok ? I mean you won't get float result in your code. Commented Aug 8, 2014 at 8:25
  • @KudoCC, It a minor thing. but focus on the overall question.. Commented Aug 8, 2014 at 8:33
  • "9/2=4" will be ok for now !!! Commented Aug 8, 2014 at 9:01

3 Answers 3

1

Perhaps you could compute it asynchronous in a background thread so that you don't block the UI.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    // Code to execute in background thread
});
Sign up to request clarification or add additional context in comments.

Comments

1

In your code, I find you don't take float result into account, so I can use shift operator instead of dividing.

// allocate enough memory
NSMutableArray * tempArray = [NSMutableArray arrayWithCapacity:[jsonArray count]];
// fast enumerate
for (NSDictionary *dict in jsonArray) {
    NSMutableDictionary * tempDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    // use shift operator instead of dividing by 2
    tempDict[@"lx"] = @([tempDict[@"lx"] intValue]>>1);
    tempDict[@"ly"] = @([tempDict[@"ly"] intValue]>>1);
    tempDict[@"mx"] = @([tempDict[@"mx"] intValue]>>1);
    tempDict[@"my"] = @([tempDict[@"my"] intValue]>>1);
    [tempArray addObject:tempDict];
}

I don't think this method will improve much performance because we must enumerate all the items, the time complexity is not changed.

2 Comments

Thanks.... +1 for your effort and good solution. But i am looking for more optimised code. Trying with setValue:ForKeyPath: now...
@NJGadhiya You can have a try but I think it does the same thing.
-1

Addressing your comment to another answer: I want to slow down the process of parsing the data:

If you want to return control to the main thread to allow other actions to happen you can use

[[NSRunLoop mainRunLoop] runUntilDate:[NSDate date]];

which will temporarily pause your current data processing loop and allow other things to happen. Obviously you can also change [NSDate date] to a date further in the future if you want to slow down more.

2 Comments

Not a great idea - using one of the mechanisms in IOS to perform work in the background should be preferred than mucking around with the runloop
@cacau, the OP said in the comment to the previous answer "I want to slow down the process of parsing the data". The code above should do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.