y 轴表示列表/数组中每个节点的平均访问时间(以ns为单位)(访问所有元素的总时间除以元素数)。
x 轴表示被迭代的数组中的元素数。
其中red是NSMutableArray
的实现,blue是我的链接列表(CHTape
)。
在每个外部循环中,每个列表/数组都附加一个空字符串@""
。在内部循环中,检索每个列表/数组中的每个字符串,这是定时和记录的。在所有时间之后,我们在Wolfram语言输出中输出以生成绘图。
NSMutableArray
如何实现如此惊人且一致的结果?如何实现类似?
我的NSFastEnumeration实施:
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])stackBuffer count:(NSUInteger)len
{
if (state->state == 0)
{
state->state = 1;
state->mutationsPtr = &state->extra[1];
state->extra[0] = (unsigned long)head;
}
CHTapeNode *cursor = (__bridge CHTapeNode *)((void *)state->extra[0]);
NSUInteger i = 0;
while ( cursor != nil && i < len )
{
stackBuffer[i] = cursor->payload;
cursor = cursor->next;
i++;
}
state->extra[0] = (unsigned long)cursor;
state->itemsPtr = stackBuffer;
return i;
}
完整的测试代码:
NSMutableArray *array = [NSMutableArray array];
CHTape *tape = [CHTape tape];
unsigned long long start;
unsigned long long tapeDur;
unsigned long long arrayDur;
NSMutableString * tapeResult = [NSMutableString stringWithString:@"{"];
NSMutableString * arrayResult = [NSMutableString stringWithString:@"{"];
NSString *string;
int iterations = 10000;
for (int i = 0; i <= iterations; i++)
{
[tape appendObject:@""];
[array addObject:@""];
// CHTape
start = mach_absolute_time();
for (string in tape){}
tapeDur = mach_absolute_time() - start;
// NSArray
start = mach_absolute_time();
for (string in array){}
arrayDur = mach_absolute_time() - start;
// Results
[tapeResult appendFormat:@"{%d, %lld}", i, (tapeDur/[tape count])];
[arrayResult appendFormat:@"{%d, %lld}", i, (arrayDur/[array count])];
if ( i != iterations)
{
[tapeResult appendString:@","];
[arrayResult appendString:@","];
}
}
[tapeResult appendString:@"}"];
[arrayResult appendString:@"}"];
NSString *plot = [NSString stringWithFormat:@"ListPlot[{%@, %@}]", tapeResult, arrayResult];
NSLog(@"%@", plot);
答案 0 :(得分:1)
通过在链接列表上强制关闭ARC相关文件,效率显着提高。它将访问时间从约70ns减少到~14ns。虽然平均来说这仍然较慢,但NSArray平均只有两倍慢,而不是十倍慢。
虽然ARC可以使代码更快,但在迭代情况下会增加不必要的释放/保留调用。
感谢Greg Parker's评论。