I need for fast reading data from standard input stream of console. Input consist of 100.000 rows with 20 chars each (2 million chars); user paste it from clipboard. My procedure works for about 3 minutes (very slowly; the target is 10 seconds). It is look like:
var inputData = new string[100000]; // 100.000 rows with 20 chars
for (int i = 0; i < 100000; i++) // Cycle duration is about 3 minutes...
{
inputData[i] = Console.ReadLine();
}
// some processing...
What's I tried:
Directly: Console.Read, Console.ReadKey - the same result
Console.In: Read(), ReadLine(), ReadAsync(), ReadLineAsync(), ReadBlock(with various block size), ReadBlockAsync(), ReadToEnd(), ReadToEndAsync() - the same result
new StreamReader(Console.OpenStandardInput(buffer)) with various buffer and block size - the same result
Hide console window at start of reading, and show it when reading is finished - acceleration 10%
I tried get input data from file - it's works perfectly and fast. But I need read from __ConsoleStream.
I noticed, while input reading in progress - process conhost.exe actively uses a processor.
How can I speed up the reading of input?
upd:
Increasing/decreasing Console.BufferHeight and Console.BufferWidth has no effect
ReadFile
msdn is also slowly. But I noticed an interesting fact:ReadFile(handle, buffer, bufferSize, out bytesCount, null); // bufferSize may be very big, but buffer obtains no more than one row (with \r\n). // So, it seems that data passed into InputStream row-by-row syncroniously.
inputData = Console.ReadLine();
won't compile and how exactly does the Clipboard fit in? – MicrometryinputData[i] = Console.ReadLine();
– SleuthDateTime.Now.Ticks
before and after callingRead()
. – Sleuth