始终更新文本还是先更新? [英] Update Text always or IF first??

查看:118
本文介绍了始终更新文本还是先更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来像是一个很愚蠢的问题,也许实际上是,但是我总是有这个线索,而且从来没有清楚得到正确答案的地方.

在性能方面,当您更新/监视字符串时,更重要的是,无论字符串是否更改,始终要对其进行更新,或者首先放置IF语句,看看字符串是否已更改,如果是,则进行更新?

可能尚不清楚,因此我们将举一个实际的例子,假设我们正在运行一个线程或一个计时器,该线程或计时器每100ms就会从Process.GetProcesses
检查进程计数

This may sound like a pretty dumb question, and maybe in fact it is, but I had always this clue and where never clear to get the correct answer.

In terms of performance, when you are updating/monitoring a string, what is more important, always update the string no matter if it changed, or first put an IF statement and see if the string has changed and if so, update?

Maybe its not clear, so we will take a practical example, assume we are running a thread or a timer that is checking,every 100ms the count of process, from Process.GetProcesses

Label1.Text = Process.GetProcesses.Count




Vs.

if Process.GetProcesses.Count <> Label1.Text Then
  Label1.Text = Process.GetProcesses.Count
end if 



VS. (我读到.等于更快.



Vs. (and I read that .Equals is faster

if Not Label1.Text.Equals(Process.GetProcesses.Count) Then
  Label1.Text = Process.GetProcesses.Count
end if 



我认为始终设置没有IF的文本应该更快的原因是您没有两次查询process.getprocesses.count类,但是另一方面,它迫使标签更新其属性...

最佳做法是什么?



The reason I assume that always setting the text without IF should be faster is that you are not querying the process.getprocesses.count class two times, but on the other hand, it''s forcing the label to update its properties...

What are the best practices to this?

推荐答案

我非常有信心
I''m pretty confident that
if Process.GetProcesses.Count <> LastCount Then
  LastCount = Process.GetProcesses.Count
  Label1.Text = LastCount   
end if



LastCountInteger变量的情况下,您的类的成员将表现更好.
:)



Where LastCount is an Integer variable, member of your class, would perform better.
:)


标签的属性设置器可能看起来像这样:
the property setter for the label probably looks something like this:
set
{
  if(text != value)
  {
    text = value;
    ...
    // some code to invalidate label causing an eventual repaint
    ...
    OnPropertyChanged("Text");
  }
}





if Process.GetProcesses.Count <> Label1.Text Then  
  Label1.Text = Process.GetProcesses.Count
end if



Process.GetProcesses.Count <> Label1.Text导致在执行字符串compare
之前将Count转换为文本.
Label1.Text = Process.GetProcesses.Count使计数再次转换为测试.

所以我想我只想赋值.

问候
Espen Harlinn



The Process.GetProcesses.Count <> Label1.Text causes Count to be converted to text before doing the string compare

Label1.Text = Process.GetProcesses.Count causes count to be converted to test once more.

So I guess I would just assign the value.

Regards
Espen Harlinn


在屏幕上绘制图形时,检查实际绘制图形的需求几乎总是比仅仅绘制图形要快得多,而不必冒着风险.这是因为绘图是非常昂贵的操作.

但是,可能是标签组件本身对其进行了检查,并且可以在实际应用新值之前在内部将旧值与新值进行比较.

另一个想法可能是;您多久更新一次标签?现在是否值得考虑?我无法想象这将是您应用程序中的瓶颈,除非您直接调用重绘(不应该这样)

好吧,希望这能给您一些想法.

祝你好运!
When it comes to drawing to screen, checking the need to do the actual drawing is almost always faster than just drawing with the risk it isn''t really necessary. This is because the drawing is a very expensive operation.

However, it could be that the label component checks this by itself and could internally check the old value against the new value before actually applying it.

Another thought might be; how often do you update the label? Is it even worth thinking about right now? I cannot imagine that it would be a bottleneck in your application unless you would call the repaint directly (which shouldn''t be done by the way)

Well, hopefully this gives you some ideas about it.

Good luck!


这篇关于始终更新文本还是先更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆