1

我有一个字典,它使用类名和选择器的组合作为键来存储对象。我正在使用以下函数来计算哈希:

+(NSString*) getKeyForClass:(Class) clazz andSelector:(SEL) selector {
    return [NSString stringWithFormat:@"%@_%@",NSStringFromClass(clazz), NSStringFromSelector(selector)];
}

在运行分析器时,我发现这个函数是计算的瓶颈。有没有更好(= 更有效)的方法来从类和选择器创建键?

4

2 回答 2

3

几个备选方案。

继续使用字符串作为键,但速度更快:

使用字符串比你真正需要的要重一些,但它至少很简单。

使用-[NSString stringByAppendingString]会更快。解析格式字符串需要做很多工作。

return [[NSStringFromClass(clazz) stringByAppendingString:@"_"] stringByAppendingString:NSStringFromSelector(selector)];

使用单个NSMutableString而不是制作中间字符串可能会更好。配置文件并查看。

NSMutableString* result = [NSStringFromClass(clazz) mutableCopy];
[result appendString:@"_"];
[result appendString:NSStringFromSelector(selector)];
return result;

使用自定义对象作为键:

您可以将自定义对象作为引用类和选择器的键。在它上面实现NSCopyingand -isEqual:-hash所以你可以将它用作字典中的键。

@interface MyKey : NSObject <NSCopying>
{
    Class _clazz;
    SEL _selector;
}

- (id)initWithClass:(Class)clazz andSelector:(SEL)selector;

@end

@implementation MyKey

- (id)initWithClass:(Class)clazz andSelector:(SEL)selector
{
    if ((self = [super init])) {
        _clazz = clazz;
        _selector = selector;
    }
    return self;
}

- (id)copyWithZone:(NSZone*)zone
{
    return self; // this object is immutable, so no need to actually copy it
}

- (BOOL)isEqual:(id)other
{
    if ([other isKindOfClass:[MyKey class]]) {
        MyKey* otherKey = (MyKey*)other;
        return _clazz == otherKey->_clazz && _selector == otherKey->_selector;
    } else {
        return NO;
    }
}

// Hash combining method from http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))

- (NSUInteger)hash
{
    return NSUINTROTATE([_clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)_selector;
}

@end

+ (MyKey*)keyForClass:(Class)clazz andSelector:(SEL)selector
{
    return [[MyKey alloc] initWithClass:clazz andSelector:selector];
}

消除中间人:

如果您永远不需要将类和选择器从关键对象中拉出,那么您可以使用上面计算的哈希值,存储在NSNumber.

// Hash combining method from http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))

+ (NSNumber*)keyForClass:(Class)clazz andSelector:(SEL)selector
{
    NSUInteger hash = NSUINTROTATE([clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)selector;
    return [NSNumber numberWithUnsignedInteger:hash];
}
于 2013-01-20T20:16:22.003 回答
0

SELs 本身是独一无二的;您可以使用NSValue并包装:

[NSValue valueWithBytes:&selector objCType:@encode(SEL)];
于 2013-01-20T19:59:01.660 回答