Looks like reading the data in tests 07 and 08 in Network Cabling puzzle in Swift takes more than 0.7 sec and causes timeout before any computation. I’m using .componentsSeparatedByString as suggested in a recent (now closed) thread.
More generally, the slow data reading in Swift seems to be (still) a bit problematic in other puzzles involving “big data”.
For example, the Horse Raicing: in Swift, in some cases there is little time left after reading and I wasn’t able to execute the same (easy) algorithm I used in Python.
@Naity, well, it’s less about general performances and more about I/O.
And as far as I remember, even in Java non-buffered I/O can be quite slow…
That said, I’ve run a simplistic benchmark for loops and integer operatiions in a couple of languages on CG.
That’s what I’ve got (less is better):
Go: 57ms
C: 85ms
Java: 274ms
JS: 427ms
Swift: 516ms
Lua: 3433ms
Python: 7220ms (estimated from a smaller sample)
Maybe other benchmarks (lists, memory intensive tasks etc.) would give different results, but this one puts Swift closer to JIT than compiled languages.
@MaximeC Thanks, but note that my problem is not with the standard code (anyway I used the newer version).
The problem is one can not even start to solve the puzzle, reading the data causes the timeout.
So a solution would be to allow for a bit more time(?).
I tried another way to read inputs but your modification happened while I did it, so I don’t know if it there was improvement in the performance. I lack knowledge in Swift to benchmark what I tried:
let stdin = NSFileHandle.fileHandleWithStandardInput()
let rawInputs = stdin.readDataToEndOfFile()
let inputs = (NSString(data: rawInputs, encoding:NSUTF8StringEncoding)!).componentsSeparatedByString("\n").map({$0.componentsSeparatedByString(" ")}).filter({$0.count > 1})
let N = inputs.count
if N > 0 {
for i in 0...(N-1) {
let X = Int(inputs[i][0])!
let Y = Int(inputs[i][1])!
}
}