質問

うーん...この問題で受け入れられるほど速くデータを読み書きする方法を見つけるのはちょっと困難です( https://www.spoj.pl/problems/intest/ )使用 F#.

私のコード( http://paste.ubuntu.com/548748/ )tleを取得します...

データの読み取りをスピードアップする方法はありますか?

役に立ちましたか?

解決

私のこのバージョンは時間制限を渡します(ただし、それでもひどく遅い〜14秒です):

open System
open System.IO

// need to change standard buffer, not to add an additional one
let stream = new StreamReader(Console.OpenStandardInput(4096))

let stdin = Seq.unfold (fun s -> if s = null then None else Some (s,stream.ReadLine())) <| stream.ReadLine()

let inline s2i (s : string) = Array.fold (fun a d -> a*10u + (uint32 d - uint32 '0') ) 0u <| s.ToCharArray()

let calc = 
    let fl = Seq.head stdin
    let [|_;ks|] = fl.Split(' ')
    let k = uint32 ks
    Seq.fold (fun a s -> if (s2i s) % k = 0u then a+1 else a) 0 <| Seq.skip 1 stdin

printf "%A" calc

このバージョンのボトルネックは実際にはそうです string -> uint32 コンバージョン(Stranding UINT32 CASTからStringからさらに遅い)読み取り自体は、サンプル入力(〜100mファイル)に約2秒(合計時間の6秒)かかりますが、それでも大きな結果ではありません。一度 s2i 命令的なスタイルで書き直されている場合、総実行時間はSPOJで10秒に減らすことができます。

let inline s2i (s : string) =
    let mutable a = 0u
    for i in 0..s.Length-1 do a <- a*10u + uint32 (s.Chars(i)) - uint32 '0'
    a

他のヒント

私は実際には知りませんが、一度にキャラクターを読むのは悪いことであり、EG 4Kを一度にバッファーに読んでからバッファーを処理する必要があると思います。

let buf =
    let raw = System.Console.OpenStandardInput()
    let bytebuf = new System.IO.BufferedStream(raw)
    new System.IO.StreamReader(bytebuf)

buf.Read()     // retrieves a single character as an int from the buffer
buf.ReadLine() // retrieves a whole line from the buffer
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top