如何优化Console.WriteLine("");

How can I optimize Console.WriteLine (“”);
2021-03-30
  •  译文(汉语)
  •  原文(英语)

碰巧我试图每秒打印数千个字符串,但是我发现我的应用程序受到影响,因为Console.WriteLine(""); 使应用程序变慢得多,是否有任何方法可以对其进行优化?

我正在使用多线程在每个IP地址中,都会实时打印报告,即 http://i.imgur.com/3PII3hi.png 访问打印服务器的所有IP地址.它们大约是每秒10,000

Console.WriteLine("   Client [" + IP + "]  are logged");
速聊1:
为什么每秒要打印数千个字符串?您的应用程序在做什么?我个人建议您在单独的线程(例如,后台工作程序)中执行此操作,但是在不知道您的应用程序的实际用途的情况下,我不建议您这样做,除非我知道您的实际操作.
速聊2:
速聊3:
每秒可以读取10,000行吗?该控制台用于用户交互.
速聊4:
不确定您了解这里的人在说什么,例如@ AlessandroD'Andria.如果您每秒要在控制台上转储"数千条消息",那是为了谁?你为什么要这么做?该窗口上的缓冲区通常小于一千,没有人能够读取这些消息.这类似于人们在问为什么普通的UI网格不能处理数百万行.答案是"因为这是一种对待用户的相当愚蠢的方式".
速聊5:
您正在记录以供将来参考吗?如果是这样,那么也许文件比控制台更适合此操作?
解决过程1

记录应该使用文件而不是控制台进行:

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

您也想将其放在try / catch块中.

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 http://i.imgur.com/3PII3hi.png all IP addresses that access the print server. They are approximately 10,000 per second

Console.WriteLine("   Client [" + IP + "]  are logged");
Talk1:
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.
Talk2:
Talk3:
You can read 10,000 line per second? The console is for user interaction.
Talk4:
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".
Talk5:
Are you logging for future reference? If so then perhaps a file is better suited for this than the console?
Solutions1

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.

转载于:https://stackoverflow.com/questions/37092253/how-can-i-optimize-console-writeline

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入