Skip to content

Instantly share code, notes, and snippets.

@TyrfingMjolnir
Created June 4, 2023 07:13
Show Gist options
  • Save TyrfingMjolnir/6d206e55c57e8b8b29e08df5440b450f to your computer and use it in GitHub Desktop.
Save TyrfingMjolnir/6d206e55c57e8b8b29e08df5440b450f to your computer and use it in GitHub Desktop.
Timing the time spend on executing through NSTask
//#!/usr/bin/swift
import Foundation
import QuartzCore
//let task = Process()
//task.launchPath = "/usr/bin/defaults"
//task.arguments = ["write","com.apple.dock","persistent-apps","-array-add","{\"tile-type\"=\"spacer-tile\";}"]
//let pipe = Pipe()
//task.standardOutput = pipe
//task.standardError = pipe
//task.launch()
//task.waitUntilExit()
//let task2 = Process()
//task2.launchPath = "/usr/bin/killall"
//task2.arguments = ["Dock"]
//let pipe2 = Pipe()
//task2.standardOutput = pipe2
//task2.standardError = pipe2
//task2.launch()
//task2.waitUntilExit()
func executionTimeInterval( block: () -> () ) -> CFTimeInterval {
let start = CACurrentMediaTime()
block();
let end = CACurrentMediaTime()
return end - start
}
let test = executionTimeInterval {
let task = Process()
// task.launchPath = "/usr/sbin/lsof"
task.launchPath = "/bin/ls"
task.arguments = ["-lAh"]
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString( data: data, encoding: String.Encoding.utf8.rawValue )! as String
print( output )
}
print( test )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment