Swift time out on reading data in Network Cabling

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.

Could it be that there is some kind of misalignment between the language name and its performance '^^

When MaximeC said: “Bug Fixed.” I thought the “default code” was changed for all puzzles.

My bad, I forgot one case in the code generator. It will be fixed later today.

@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(?).

BTW, it’s great to have Swift on CG, thanks!!

I’m working on it. I need to learn the language first :D.

I can reproduce. We will increase the time for Swift…

1 Like

Great, thanks!

It should be fine. Even using the split…

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])!

To measure the elapsed time I use the following code

import CoreFoundation let startTime = CFAbsoluteTimeGetCurrent ... let endTime = CFAbsoluteTimeGetCurrent() debugPrint("Elapsed \(endTime - startTime) sec..", toStream: &errStream)

In my tests (Network Cabling, Example 7 and 8) @yohannjardin’s code takes about 1 sec while

(readLine()!).componentsSeparatedByString(" ")

takes 0.71 sec and the standard

(readLine()!).characters.split{$0 == " "}.map(String.init)

takes 0.75 sec in Example 7 and, quite surprisingly, 1.15 sec in Example 8.

PS. I confirm, no more timeout problems in Swift – thanks!

1 Like