简体   繁体   English

NSMutable阵列高CPU使用率

[英]NSMutable Array High CPU Usage

I recently change c array to NSMutable array because I had a lot of troubles with memory. 我最近将c数组更改为NSMutable数组,因为我在内存方面遇到很多麻烦。 However it works, but starting from 但是它有效,但是从

/* If two starttimes are within interval millisec, make them the same */
for (int i = 0; i < [starttimes count] - 1; i++) {
   if ([[starttimes objectAtIndex:i+1] integerValue] - [[starttimes objectAtIndex:i] integerValue] <= interval) {
        [starttimes insertObject: [starttimes objectAtIndex:i] atIndex:(i+1)];
    }
}

CPU usage is nearly 100%. CPU使用率接近100%。 I do not know what is going on? 不知发生了什么? The logic is fairly simple. 逻辑很简单。 All it does is to get two elements from this NSMutable array, then compare and insert. 它所做的就是从此NSMutable数组中获取两个元素,然后进行比较和插入。 BTW, this chunk of codes never stop. 顺便说一句,这段代码永远不会停止。 It keeps running 3 seconds, then my app crashes. 它持续运行3秒钟,然后我的应用程序崩溃了。

(TimeSignature*)time {
/* Get all the starttimes in all tracks, in sorted order */
NSInteger initsize = 1;
if ([tracks count] > 0) {
    MidiTrack *track = [tracks objectAtIndex:0];
    initsize = [track.notes count];
    initsize = initsize * [tracks count]/2;
}
NSMutableArray* starttimes = [[NSMutableArray alloc]initWithCapacity:initsize];

for (int tracknum = 0; tracknum < [tracks count]; tracknum++) {

    NSLog(@"tracknum is %d",tracknum);
    MidiTrack *track = [tracks objectAtIndex:tracknum];
     NSLog(@"WE ARE HERE %ld",(long)initsize);
    for (int j = 0; j < [track.notes count]; j++) {
        MidiNote *note = [track.notes objectAtIndex:j];

        NSLog(@"%@",note);
        [starttimes addObject:[NSNumber numberWithInteger:note.startTime]];
    }
}


/* Notes within "millisec" milliseconds apart should be combined */
int interval = time.quarter * millisec * 1000 / time.tempo;

/* If two starttimes are within interval millisec, make them the same */
for (int i = 0; i < [starttimes count] - 1; i++) {
    if ([[starttimes objectAtIndex:i+1] integerValue] - [[starttimes objectAtIndex:i] integerValue] <= interval) {
        [starttimes insertObject: [starttimes objectAtIndex:i] atIndex:(i+1)];
    }

}

Thanks so much 非常感谢

General Objective-C advice: read the method names. 通用的Objective-C建议:阅读方法名称。 They tell you want the methods do. 他们告诉您想要方法。

You've misunderstood -insertObject: . 您误解了-insertObject: What insertObject does is... insert an object. insertObject作用是... 插入对象。 What it does not do: replace an object. 它不起作用:替换对象。 You're increasing the size of the array every time you call insertObject . 每次调用insertObject时,都会增加数组的大小。 In practice you're creating an infinite loop should the if condition pass even once. 在实践中, if条件一次甚至通过, if您将创建一个无限循环。

You probably wanted -replaceObjectAtIndex:withObject: . 您可能需要-replaceObjectAtIndex:withObject: Which, like insertObject: , will do what the name says. 就像insertObject: ,它将按照名称中的说明进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM