Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中objective-c方法的性能优化_Ios_Objective C_Performance - Fatal编程技术网

iOS中objective-c方法的性能优化

iOS中objective-c方法的性能优化,ios,objective-c,performance,Ios,Objective C,Performance,我有这样的方法,很难对其进行优化以使其运行得更好。在NSLog(@“Debug2”)和NSLog(@“Debug3”)之间,大约需要1秒和4次100个单元。在iPhone上只需2秒。单位越多,需要的时间就越多。250台,是mac电脑1.7秒和iphone手机3.2秒的5倍 我不认为表现取决于一个循环中的一个循环。当250*5=1250时,计算机处理速度不应太快 方法思路: 输入:NSMutableArray时间-包含时间(2012-12-122013-01-19)等 NSMutableArray

我有这样的方法,很难对其进行优化以使其运行得更好。在
NSLog(@“Debug2”)
NSLog(@“Debug3”)
之间,大约需要1秒和4次100个单元。在iPhone上只需2秒。单位越多,需要的时间就越多。250台,是mac电脑1.7秒和iphone手机3.2秒的5倍

我不认为表现取决于一个循环中的一个循环。当250*5=1250时,计算机处理速度不应太快

方法思路:

输入:NSMutableArray时间-包含时间(2012-12-122013-01-19)等 NSMutableArray objectArray-包含
LogUnit
对象。 输出:计算视图的数组

主要思想:使用ObjectArray中时间相同的对象为每次创建一个视图

我通过显示一小部分对象(请参见代码中的
计数的
\u 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;
}

有几件事你可以试试

首先是分析:

  • 在仪器中试用轮廓仪
  • 如果您对profiler有些不满,那么尝试获取代码每个部分的平均时间
现在进行优化:

  • 避免在循环中创建完全相同的对象。尝试创建示例对象,然后重用或复制它们;
    • 创建一个示例
      ui视图
      ,并在需要新视图时将其存档和取消存档。这也适用于
      ui标签
    • 尽管我从未使用过它,但有一种方法可以创建一个
      NSPredicate
      ,并在以后应用一些值。这样您就可以重用相同的
      ui谓词
    • 将您需要的颜色也保存在一个变量中,而不是要求
      UIColor
      ,因为我们不知道
      UIColor
      如何处理它
  • 您能否将内部
    短路至
    回路,即您拥有最多正确的单元数
嗯,剖析器应该足以让您了解瓶颈在哪里,但我也让您了解一些想法


别忘了在这里报告结果!:)

有几件事你可以试试

首先是分析:

  • 在仪器中试用轮廓仪
  • 如果您对profiler有些不满,那么尝试获取代码每个部分的平均时间
现在进行优化:

  • 避免在循环中创建完全相同的对象。尝试创建示例对象,然后重用或复制它们;
    • 创建一个示例
      ui视图
      ,并在需要新视图时将其存档和取消存档。这也适用于
      ui标签
    • 尽管我从未使用过它,但有一种方法可以创建一个
      NSPredicate
      ,并在以后应用一些值。这样您就可以重用相同的
      ui谓词
    • 将您需要的颜色也保存在一个变量中,而不是要求
      UIColor
      ,因为我们不知道
      UIColor
      如何处理它
  • 您能否将内部
    短路至
    回路,即您拥有最多正确的单元数
嗯,剖析器应该足以让您了解瓶颈在哪里,但我也让您了解一些想法


别忘了在这里报告结果!:)

首先感谢你的回答和帮助。你能说我应该试试探查器中的哪种仪器吗?我对探查器有点陌生?对于正确的单位,单位为空的可能性很小。我可能不会检查此项,但它不会大大提高性能。。至于其他几点,我将在几分钟后尝试:)时间分析器。是的,刚刚发现LogUnitViewDidLoad方法,在时间上花费了很多精力。请检查那里的代码。但是我想你将无法避免调用该方法,因为你需要检查视图框架。我在LogUnit的viewDidLoad方法中优化了代码,它确实帮我节省了很多时间。谢谢你的回答,它确实帮助我找到了解决方案!首先感谢你的回答和帮助。你能说我应该试试探查器中的哪种仪器吗?我对探查器有点陌生?对于正确的单位,单位为空的可能性很小。我可能不会检查此项,但它不会大大提高性能。。至于其他几点,我将在几分钟后尝试:)时间分析器。是的,刚刚发现LogUnitViewDidLoad方法,在时间上花费了很多精力。请检查那里的代码。但是我想你将无法避免调用该方法,因为你需要检查视图框架。我在LogUnit的viewDidLoad方法中优化了代码,它确实帮我节省了很多时间。谢谢你的回答,它确实帮助我找到了解决方案!