我有这样的方法并且很难优化它以更好地运行。 100个单位大约需要1秒,NSLog(@"Debug2")
和NSLog(@"Debug3")
需要4次。在iPhone上2秒。拥有更多单位需要更多。 250个单位和5次,在Mac上需要1,7秒,在iPhone上需要3秒。
我认为性能不在于一个周期内的循环。当250 * 5 = 1250时,计算机不应该太快处理。
方法理念:
输入:NSMutableArray次 - 包含次数(2012-12-12,2013-01-19)等
NSMutableArray objectArray - 包含LogUnit
个对象。
输出:计算视图的数组。
主要思想:每次使用来自ObjectArray的对象的时间相同的视图。
我通过显示一小部分对象(请参阅代码中的counted
和_breakPlease
)让它变得更快,但它仍然不够快。
- (NSMutableArray*) sortViews: (NSMutableArray *)times and:(NSMutableArray *)objectArray
{
NSLog(@"debug2");
NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:times.count];
NSString *number = [NSString stringWithFormat:@"SortLog%i ",[[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"].intValue];
NSString *comp = [[NSUserDefaults standardUserDefaults] stringForKey:number];
LogUnit *unit;
int counted = 0;
for(int x = 0; x != times.count; x++)
{
@autoreleasepool {
UIView *foo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
foo.backgroundColor = [UIColor clearColor];
int f = 0;
int h = 0;
NSString *attributeName = @"realTime";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@", attributeName, [times objectAtIndex:x]];
// Filter the objectArray by the time, so we don't have to run through all the objects in objectArray, and check if time is right.
NSArray *filtered = [objectArray filteredArrayUsingPredicate:predicate];
for(int i = 0; i != filtered.count; i++)
{
unit = [filtered objectAtIndex:i];
// Filter units by user choice (comp). Like comp = Warnings or comp = Errors and etc.
if([[unit status] isEqualToString:comp] || [comp isEqualToString:NULL] || comp == NULL)
{
// testing if the unit is correct to use
if([unit getEvent] != NULL)
{
// below is some computation for frames, to get them to proper position
unit.view.frame = CGRectMake(unit.view.frame.origin.x, f * unit.view.frame.size.height + 30, unit.view.frame.size.width, unit.view.frame.size.height);
[foo addSubview:unit.view];
counted++;
f++;
h = unit.view.frame.size.height * f;
}
}
}
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.font = [UIFont boldSystemFontOfSize:20];
myLabel.textColor = [UIColor colorWithRed:88 green:154 blue:251 alpha:1];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.text = [times objectAtIndex:x];
// Just adding a label with "2012-12-30" or etc on top of the view.
[foo addSubview:myLabel];
foo.frame = CGRectMake(0,0, 320, h + 30 );
h = 0;
[views addObject:foo];
// counting added views for performance, if more than 30, return, and show only small portion of whole units, user has the option to show all the units if he wants to, but that takes a time to load..
if(counted > 30 && filtered.count != counted)
{
if(_breakPlease == false)
{
_broken = true;
break;
}
}
}
}
NSLog(@"debug3");
return views;
}
答案 0 :(得分:4)
你可以试试几件事。
首先进行分析:
现在进行优化:
UIView
并将其存档并在需要新视图时将其归档。也可以将其应用于UILabel
; NSPredicate
并稍后应用某些值。这样您就可以重复使用相同的UIPredicate
; UIColor
询问,因为我们不知道UIColor
如何处理它; for
循环,即您拥有最大数量的正确单位?好吧,分析器应该足以让你了解你的瓶颈在哪里,但我也会给你一些想法。
别忘了在这里报告结果! :)