我是新的ios,我不知道NSThread在IOS上的多线程性能。 我有个函数多线程很慢?
-(void) writeBlock:(NSString *)srcPath toPath:(NSString *)destPath fromIndex:(int)fIndex toIndex:(int)tIndex
{
//create file handle for srcPath and destPath
for(int i=fIndex;i<=tIndex;i++){
//read from source filehandle
//write to dest filehandle
}
//close filehandle.
}
如果我创建用于上述功能的单个线程,用于执行此功能的时间为1秒。 但是,当我创建:
for(int i=0;i<20;i++)
{
[NSThread detachNewThreadSelector: @selector(writeBlock:) toTarget:self withObject:myObject];
//for each thread, tIndex-fIndex is the same with tIndex-fIndex of single above thread
}
的时间执行20个线程是13-16seconds。 我觉得时间会是1-5秒。多线程很慢? 任何人都可以帮助我吗?非常感谢
有一些与产卵新线程有关的开销。另外,如果所有这些线程都试图从文件系统读取(可能是同一个文件??),那么它们在争用文件系统访问权限时也会引入额外开销。 – UIAdam