如何监控长计算?
我想设置一个计数器来通知我有关长时间迭代计算的信息(例如在 for
中)。
是否可以设置此计数器,以便当它在屏幕上更新时,它会替换以前的值?
也就是说,打印 for
的迭代器变量是不行,因为 Matlab 要么将其打印到新行中,要么打印在前一个值之后,但经过 10000 次迭代后,屏幕将被填充。另外,我想每轮更新计数器。
I would like to set up a counter that informs me about a long iterative computation (e.g. in for
).
Is it possible to set up this counter in a way that when it is updated on screen, it replaces the previous value?
That is, printing the iterator variable of a for
is not ok, since Matlab either prints it into a new line, or after the previous value, but after 10000 iterations the screen would be filled either way. Also, I would like to update the counter in each turn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
\b
打印退格字符。例如:You can use
\b
to print a backspace character. e.g.:我不久前做了这个函数,它画了一个漂亮的ascii进度条。基本上与你的问题的其他两个答案的想法相同,但封装得更多
如果你的 for 循环很大,并且每次传递都很短,它会增加大量的计算时间开销,所以只每 100 个循环调用它迭代,或者根据需要。
I made this function a while ago, it draws a nice ascii progress bar. Basically the same idea as the other two answers to your question, but a bit more packaged-up
If your for loop is enormous, and each pass is short, it will add a lot of overhead computation time, so only call it every 100 loop iterations, or as need be.
您还可以使用 waitbar() 函数。虽然有点慢,但是看起来不错。
You can also use the waitbar() function. It is a bit slow, but looks nice.
无论您要使用哪种风格的等待栏,我建议定义一个等待栏的接口并实现它。
因此,计算函数和 GUI 绘图之间的耦合是松散的。
通过这种方式,您可以:
WaitBar
的实现,而无需修改计算函数。WaitBar
,并按需切换它们WaitBar
。No matter what style of waitbar you are going to use, I suggest defining an interface of waitbars, and implementing it.
Thus, you get loose coupling between the function that calculates and the GUI drawing.
In this way you can:
WaitBar
without modifying the calculating function.WaitBar
s easily, and switch them on demandWaitBar
in cases you don't want to show anything.