-3

It happens that I'm trying to print thousands of strings per second, but I see that my application is affected because Console.WriteLine (""); makes the application much slower, is there any way to optimize it?

I'm working with multi threads In each IP address a report is printed in real time https://i.stack.imgur.com/Lozjj.png all IP addresses that access the print server. They are approximately 10,000 per second

Console.WriteLine("   Client [" + IP + "]  are logged");
  • Can you provide some code how you are manipulating strings etc. – Panu Oksala May 07 '16 at 18:37
  • IO opration always are slow try to youse asynchrone writeline – Mehdi May 07 '16 at 18:37
  • 1
    Why are you printing thousands of strings per second? What's your application doing? I'd personally recommend that you do it inside a separate thread (say, a background worker), but without knowing the actual purpose of your application I don't want to recommend that until I know what you're actually doing. – AStopher May 07 '16 at 18:37
  • 1
    Possible duplicate of [Console.Writeline Effect on Performance](http://stackoverflow.com/questions/18464833/console-writeline-effect-on-performance) – Mehdi May 07 '16 at 18:39
  • Who's going to be reading thousands of lines a second? Do you really need to print them to a screen? – TZHX May 07 '16 at 18:46
  • @cybermonkey All IP addresses that access the print server. They are approximately 10,000 per second example: Console.WriteLine ("Client [" + IP + "] are logged"); – Sergio Garcia Lopez May 07 '16 at 18:46
  • @SergioGarciaLopez Why though? If this is for an enterprise, it sounds like you need to pool the printer server. – AStopher May 07 '16 at 18:48
  • 5
    You can read 10,000 line per second? The console is for user interaction. – Alessandro D'Andria May 07 '16 at 18:50
  • @AlessandroD'Andria Check this picture http://i.imgur.com/3PII3hi.png – Sergio Garcia Lopez May 07 '16 at 19:03
  • 4
    Not sure you understand what people here, like @AlessandroD'Andria, is saying. If you're dumping "thousands of messages" on the console every second, who is that for? Why are you doing that? The buffer on that window is typically less than a thousand, nobody is going to be able to *read* those messages. This is akin to people asking why a normal UI grid isn't able to handle millions of rows. The answer is "because that's a rather stupid way to treat your users". – Lasse V. Karlsen May 07 '16 at 19:28
  • 2
    Are you logging for future reference? If so then perhaps a file is better suited for this than the console? – Lasse V. Karlsen May 07 '16 at 19:29

1 Answers1

0

Logging should be done with a file, not the console:

private void writeToLogFile(string pathToLogFile)
    {
        File.AppendAllText(pathToLogFile, "Client [" + IP + "] are logged");
    }

Also you want to put that in a try/catch block.

Kulu
  • 111
  • 10