self weight in instrument

问题

我们知道 weight 表示执行某方法消耗的时间,但是一直不是很明确 self weight 的具体含义,之前查过一些资料: objective c - Profiling with Instruments : Whats the difference b/w self weight and weight - Stack Overflow -w250 里面提到「Self weight is the weight without the time in the children」但是我一直不明确这里的「children」是什么意思,所以还是决定自己试一下

实验

记得将「hide system libraries」打开,这样看更明确 首先创建两个类 superclass 和 subclass,superclass 内部只调用系统方法

@implementation superclass
- (instancetype)init {
    if (self = [super init]) {
        for (long i = 0; i < 10000000000; i ++) {
            NSLog(@"12312321");
        }
    }
    return self;
}
@end

@implementation subclass
- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}
@end

得到以下数据: -w250 可以看到 [superclass init] 方法的 self weight 值基本与 weight 的值一致了,说明在勾选了「hide system libraries」的情况下,self weight 表示的是这个方法本身消耗的时间,也就是方法内部所有的系统方法所消耗的时间。

为了进一步验证这个结论,将 superclass 的实现改一下,在 init 方法中写了一段耗时的操作,且特地调用了自定义的方法

@implementation superclass
- (instancetype)init {
    if (self = [super init]) {
        for (long i = 0; i < 10000000000; i ++) {
            [self keychain];
        }
    }
    return self;
}

- (void)keychain {
    [SAMKeychain passwordForService:@"xin_keychain_test" account:@"xin"];
}
@end

得到数据: -w250 可以看到 [superclass init] 方法的 self weight 值大大减小了,相应的 [SAMKeychainQuery fetch:] 的 self weight 值很大,因为此方法内部耗时的地方大部分在系统的调用方法上了,所以基本上可以印证我们上面的结论

结论

Self weight 表示的是这个方法本身消耗的时间,也就是方法内部所有的系统方法所消耗的时间。 回到一开始提到的「Self weight is the weight without the time in the children」,个人认为这里的 children 应该是方法内部调用的非系统方法