0

i'm new ios and i don't know performance of NSThread an multithread on IOS. I've a function

-(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.
}

If i create a single thread for above function,the time for perform this function is 1 second. But when i create:

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
}

The time for perform 20 threads is 13-16seconds. I think the time will be 1-5 seconds.Multi thread is slow? Can anyone help me?Thanks so much

The Bird
  • 397
  • 2
  • 8
  • 19
  • 1
    There is some overhead associated with spawning new threads. Also, if all of these threads are trying to read from the filesystem (possibly the same file??), that is also going to introduce additional overhead as they contend for file system access. – UIAdam May 13 '12 at 18:49

2 Answers2

0

Thats because you try to read and write files on a disk. There is only one disk. So if you try to read and write one disk 20 times at once. it is slow. For example if you copy 20 files to an USB-stick it is pretty fast. But if you try to copy the 20 one by one (dragging them to the USB-stick one by one, while the previous file is not copied yet) it will be very slow.

Justin
  • 2,960
  • 2
  • 34
  • 48
0

Single-Core smartphone processors can only handle so much. Creating 20 threads on the iPhone will considerably slow performance down - creating threads is expensive. Another option to creating a low-level NSThread is using an NSOperation or NSOperationQueue - see also this this thread on StackOverflow.

Moreover, writing/reading to/from memory can only be done by one thread at a time.

Community
  • 1
  • 1
David Ganster
  • 1,985
  • 1
  • 18
  • 26