0

I created a sh/Python script as execute file. However, when I run this script by one click button from Objective C program using NSTask the progress time is much slower than I execute script directly from MacOS Terminal.

I read some information from stackoverlfow to use nice -n but it doesn't help. Any one has idea how to speed up process when execute script through Objective C.

Code To Execute script:

- (NSString *)runCommand:(NSString*) script{
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];

NSArray *arguments = [NSArray arrayWithObjects:
                      @"-c" ,
                      [NSString stringWithFormat:@"%@", script],
                      nil];
[task setArguments:arguments];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];

NSPipe* stdErrPipe = nil;
stdErrPipe = [NSPipe pipe];
[task setStandardError: stdErrPipe];

NSFileHandle *file = [pipe fileHandleForReading];
NSFileHandle *filErr = [stdErrPipe fileHandleForReading];

[task launch];
NSData *data = [file readDataToEndOfFile];
NSData *dataError = [filErr readDataToEndOfFile];
[task waitUntilExit];

NSInteger exitCode = task.terminationStatus;

if (exitCode != 0)
{
    NSString *outputErr = [[NSString alloc] initWithData:dataError encoding:NSUTF8StringEncoding];
    NSLog(@"Error!");
    return [@"Error!" stringByAppendingString:outputErr];
}
NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return output;
}
l'L'l
  • 44,951
  • 10
  • 95
  • 146
Jacky Le
  • 51
  • 9
  • Are you benchmarking a release build without debugger attached? – Kamil.S Oct 22 '20 at 19:07
  • Im not sure, In Scheme setting I already keep build as Release, any other setting I need to set ? – Jacky Le Oct 23 '20 at 04:28
  • Set the ```NSTask.qualityOfService``` to ```NSQualityOfServiceUserInteractive``` or ```NSQualityOfServiceUserInitiated``` and see how it goes. – skaak Oct 31 '20 at 13:19

0 Answers0