How to read data faster?

Hmmm ... its calling is harder to find a way to read / write data faster to get ACCEPT in this problem ( https://www.spoj.pl/problems/INTEST/ ) using F # .

My code ( http://paste.ubuntu.com/548748/ ) gets TLE ...

Any ideas on how to speed up data reading?

+4
source share
3 answers

This version of mine goes through a time limit (but still awfully slow ~ 14 seconds):

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 ad -> 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 as -> if (s2i s) % k = 0u then a+1 else a) 0 <| Seq.skip 1 stdin printf "%A" calc 

Despite the fact that the bottleneck of this version is actually converted to string -> uint32 (the standard output of uint32 from a string is even slower), the reading itself takes about 2 seconds (versus 6 seconds of total time) on my input example (file ~ 100 M) - still not a great result. When s2i rewritten in an imperative style, the total execution time can be reduced to 10 seconds by spoj:

 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 
+4
source

I really don't know, but I would suggest that reading from a character at a time is bad, and you should read, for example. 4k to the buffer at a time, and then process the buffer.

+2
source
 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 
+1
source

Source: https://habr.com/ru/post/1333773/


All Articles