I'm using a Swift function that successfully loads data from a text file into an Double array, but it is slow. Is there a way to load numeric data directly without using the String initializer that may be faster? Or any other suggestions to speed this up?
func arrayFromContentsOfFileWithPath(path: String) -> [Double]? {
do {
let content = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding)
let stringArray = content.componentsSeparatedByString("\n").map{
$0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
return stringArray.map{Double($0)}.flatMap{$0}
} catch _ as NSError {
return nil
}
}
EDIT:
To quantify things a bit, the data file is 10000 samples and the load time is 0.183 s for a single load (according to a measureBlock in my unit tests). In comparison, MATLAB loads the file in 0.033 s. Here are the first few samples of the data:
8.1472369e-01
9.0579194e-01
1.2698682e-01
9.1337586e-01
6.3235925e-01
9.7540405e-02
2.7849822e-01
5.4688152e-01
9.5750684e-01
9.6488854e-01
UPDATE:
Following @appzYourLife's advice to combine the mappings (I used .flatMap{Double($0)}) and to use a Release build, the load time is now 0.119 s. Much better, but still about 4x the time of MATLAB, which was very unexpected.