更快地打印控制台字符

编程入门 行业动态 更新时间:2024-10-24 16:22:31
本文介绍了更快地打印控制台字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个接受数字的函数,该函数会将其转换为如下所示的数字的5x7图形表示形式:

I have a function that accepts a number which will convert it to a 5x7 graphic representation of digits like this:

Console.WriteLine(" ███ "); // byte: 0000 1110 Console.WriteLine("█ █"); // 0001 0001 Console.WriteLine("█ █"); // 0001 0001 Console.WriteLine("█ █"); // 0001 0001 Console.WriteLine("█ █"); // 0001 0001 Console.WriteLine("█ █"); // 0001 0001 Console.WriteLine(" ███ "); // 0000 1110

有人告诉我使用字节数组可以更快地完成操作.这基本上就是我所做的:

I was told it can be done faster using byte arrays. This is basically what I did:

byte[] data = new byte[] { 14, 17, 17, 17, 17, 17, 14 }; BitArray bitData = new BitArray(data); int bitCounter = 0; foreach (bool bit in bitData) { if (bit) Console.Write("█"); else Console.Write(" "); bitCounter++; if (bitCounter>7) { bitCounter=0; Console.WriteLine(); } }

这比开始时要慢,所以我不能使用它.谁能告诉我更好的方法?

This is slower than what is started with so I can't use it. Can anyone show me a better way?

更新:StringBuilder确实有帮助,但是它会比我原来的代码慢一些.它仍然基本上在控制台中使用了7次write命令,这让我很慢.我尝试使用\ n仅使用1个写命令,但是我需要缩进下一行而不擦除缩进空格内的内容.

Update: StringBuilder does help, but it will be a bit slower than the original code I had. Its still basically using the write command 7 times in the console is slowing me down. I tried using using just 1 write command using \n, but I need to indent the next line without erasing inside the indented spaces.

推荐答案

I最小化了IO.这是下面的版本,仅在最后进行控制台调用.

I Minimized IO. Here is a version below that makes console calls only at the end.

var stringBuilder = new StringBuilder(); bitCounter = 0; foreach (bool bit in bitData) { if (bit) stringBuilder.Append("█"); else stringBuilder.Append(" "); bitCounter++; if (bitCounter > 7) { bitCounter = 0; Console.WriteLine(stringBuilder.ToString()); stringBuilder.Clear(); } }

更多推荐

更快地打印控制台字符

本文发布于:2023-11-28 09:05:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   更快   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!