- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个简单的命令行程序,它有一个像素类,并将其显示到控制台是最慢的部分。我可以使用多个线程来加快写入控制台的速度吗?
你的 Vector2D 类只拥有一个 x 和 y 位置。如果 x 和 y 与比较值相同,则 equals 函数返回
像素渲染器功能看起来有点像
public Vector2D location { get; private set; }
public char pixel { get; private }
public ConsoleColor FG { get; private }
public ConsoleColor BG { get; private }
public int layer { get; private }
public int ID { get; private set; }
public void renderer()
{
foreach (Pixel pixel in GlobalPixels)
{
if((this.ID != pixel.ID) && (this.location == pixel.location))
{
if (this.layer < pixel.layer) return;
}
}
Console.SetCursorPosition(location.x, location.y);
Console.ForegroundColor = FG;
Console.BackgroundColor = BG;
Console.Write(pixel);
}
最佳答案
多线程不太可能导致大幅增加,因为 Console
操作都由并发锁控制,这将导致线程相互限制。
如果您需要加速控制台渲染,我建议您实现一个缓冲区(例如 char[80,25]
或类似的东西)并在那里写入您的像素。完成像素写入后,将整个缓冲区一次性复制到控制台,顺序从上到下,从左到右。控制台在按顺序写入行时要快得多,而当您必须使用 SetCursorPosition
时会更慢。反复。
这是一个简单的示例,可以帮助您入门:
public class BufferedConsole
{
private class Cell
{
public char Pixel;
public ConsoleColor ForegroundColor;
public ConsoleColor BackgroundColor;
}
static private Cell[,] _buffer = new Cell[80, 25];
static BufferedConsole()
{
Clear();
}
static void Clear()
{
for (int row = 0; row < 25; row++)
{
for (int col = 0; col < 80; col++)
{
_buffer[col, row] = new Cell
{
ForegroundColor = ConsoleColor.Black,
BackgroundColor = ConsoleColor.Black,
Pixel = '?'
};
}
}
Refresh();
}
public static void Plot(int x, int y, char pixel, ConsoleColor foreColor, ConsoleColor backColor)
{
var cell = _buffer[x, y];
cell.Pixel = pixel;
cell.ForegroundColor = foreColor;
cell.BackgroundColor = backColor;
}
public static void Refresh()
{
Console.SetCursorPosition(0, 0);
ConsoleColor lastForeColor = ConsoleColor.White;
ConsoleColor lastBackColor = ConsoleColor.Black;
Console.CursorVisible = false;
for (var row = 0; row < 25; row++)
{
for (var col = 0; col < 80; col++)
{
var cell = _buffer[col, row];
if (lastForeColor != cell.ForegroundColor)
{
Console.ForegroundColor = cell.ForegroundColor;
lastForeColor = cell.ForegroundColor;
}
if (lastBackColor != cell.BackgroundColor)
{
Console.BackgroundColor = cell.BackgroundColor;
lastBackColor = cell.BackgroundColor;
}
Console.Write(cell.Pixel);
}
Console.WriteLine();
}
Console.CursorVisible = true;
}
}
Console.SetCursorPosition(location.x, location.y);
Console.ForegroundColor = FG;
Console.BackgroundColor = BG;
Console.Write(pixel);
BufferedConsole.Plot(location.x, location.y, pixel, FG, BG);
Refresh()
.
关于C#做更多的线程加速写入控制台而不相互干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59637345/
这是我的 + 部分: function insert(){ var linkElement = document.getElementById("BackButton"); var linkEl
我有两辆 buggy 在轨道上移动,它们都使用超声波测量模块来检测路径中的障碍物,并由 Arduino 微 Controller 控制。两个超声波传感器以相同的频率工作,并且这个频率不能改变。两个超声
希望能快速解决这个问题。我有一个用文件位置替换特定值的脚本。不幸的是,该位置似乎经常包含\n 或 n\(因为当前目录位于临时文件夹中),导致该行中断或从该行中完全删除自身,从而使文件夹位置无效。 临时
我使用的是 Mac OSX 10.10.1。我通常使用brew或pip来安装所有东西。我注意到我的 virtualenv 总是抓取我本地安装的所有 Python 包。我通过删除我的 $PYTHONPA
我正在尝试使用 str_replace 将两个字母的语言缩写转换为完整的单词。我遇到的问题是它们在回显时会相互影响。 $lang = str_replace("en", "English", $lan
我已经创建了用于结束通话的 aidl 文件 ITelephony.aidl。现在,在通话结束之前,我需要生成正在进行的通话中的语音,以便听众认为接下来的工作有问题。我尝试通过三种方式自己解决这个问题:
使用这个查询,我得到了我需要的每个客户下的订单数量的结果。但是,当我在 SELECT 字段中使用 SUM(OrderLine.ActualPrice) 并使用内部联接将 Order 表链接到包含价格的
在 Spring Boot 应用程序中,仅使用注释,我想实现安全性 我已将 @EnableGlobalMethodSecurity(jsr360Enabled=true) 添加到配置类。该类还有一个
我一直在 Linux 下开发一个多线程程序,对信号处理有特殊要求。例如,我需要程序打印单个 Ctrl-C 输入的统计信息。 我使用 pthread_sigmask 和 sigwait 实现了这一点,即
作为我第一次涉足 Dynamic Scala 领域,我想我会尝试通过 applyDynamic 访问 bean 属性。 我的第一个非常粗略的剪裁是 trait BeanProperties ext
我知道这是一个令人沮丧的话题,但我尝试遵循文档和其他类似的帖子。 我的问题是,除非重新加载页面,否则 JS 将无法工作。 我有以下 JS: $(function() { $('a.form-sho
我正在使用 JQuery Ajax 从服务器获取脚本。 $.ajax({ url: src, type: "GET", dataType: "script", time
我正在尝试根据每个产品的选定数量分别输出计算价格。我尝试复制代码并重命名所有变量,但输出是由各种增加/减少按钮触发的。 这是目前的代码: $(".incr-btn_mobile").on("click
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: jQuery in Greasemonkey 1.0 conflicts with websites usi
我在一个 iOS 应用程序 (Obj-C) 中有一个 View ,它在中心有一个 ImageView ,紧接着在其下方有一个 slider 。 ImageView 显示专辑封面, slider 可用于
我有一个 View Controller ,将 UITapGestureRecognizer 附加到它的主 UIView,并使用户能够点击屏幕以使状态、导航和工具栏重新出现/消失(如照片应用程序)。我
我正在尝试在我的应用程序中设置 log4j,但我的一个第三方 JAR 似乎以某种方式不断将我的日志级别设置为“关闭”。它仅在执行该库中的代码时发生。我不知道它是如何做到这一点的,因为我没有使用它知道名
我有一个 UIScrollView,其框架与包含的 View Controller 相同。 当 ScrollView 的缩放比例大于 1 时,从屏幕左侧 50% 开始从左向右滑动会导致 interac
我是网页设计的新手,最近接触了 Bootstrap。我最近也开始使用 Codepen。在 Codepen 中完成一个元素后,我将代码从那里复制并粘贴到 Sublime 中,无论出于何种原因,它更改了我
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
我是一名优秀的程序员,十分优秀!