Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. (and I read that .Equals is faster
if Not Label1.Text.Equals(Process.GetProcesses.Count) Then
  Label1.Text = Process.GetProcesses.Count
end if 


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?
Posted
Updated 10-Jan-11 23:43pm
v2

I'm pretty confident that
VB
if Process.GetProcesses.Count <> LastCount Then
  LastCount = Process.GetProcesses.Count
  Label1.Text = LastCount   
end if


Where LastCount is an Integer variable, member of your class, would perform better.
:)
 
Share this answer
 
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


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
 
Share this answer
 
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900