开发者

Should I create another variable instead of 3 explicit casts?

This is a question in the matter of speed. I have the following code:

if (MyXMLReader.SearchForValue(command, new List<string>() { "/Command/Descriptions/Description/System" }, ((TabDocument)dockManager.ActiveDocument).version))
{
    txtBox_desc.Text = MyXMLReader.GetValue(command, "/Command/Descriptions/Description[" + MyXMLReader.SearchForValue_Int(command, new List<string>() { "/Command/Descriptions/Description/System" }, ((TabDocument)dockManager.ActiveDocument).version) + "]/Content");
}
if (MyXMLReader.SearchForValue(command, new List<string>() { "/Command/Uses/Use/System" }, ((TabDocument)dockManager.ActiveDocument).version))
{
    txtBox_use.Text = MyXMLReader.GetValue(command, "/Command/Uses/Use[" + MyXMLReader.SearchForValue_Int(command, new List<string>() { "/Command/Uses/Use/System" }, ((TabDocument)dockManager.ActiveDocument).version) + "]/Content");
}
if (MyXMLReader.SearchForValue(command, new List<string>() { "/Command/Notes/Note/System" }, ((TabDocument)dockManager.ActiveDocument).version))
{
    txtBox_notes.Text = MyXMLReader.GetValue(command, "/Command/Notes/Note["开发者_如何学Go + MyXMLReader.SearchForValue_Int(command, new List<string>() { "/Command/Notes/Note/System" }, ((TabDocument)dockManager.ActiveDocument).version) + "]/Content");
}

Should I create a new string variable to hold

((TabDocument)dockManager.ActiveDocument).version?

Would this benefit with more speed and responsiveness? Will this be bad or good?


In short, yes.

The cost of repeatedly casting the ActiveDocument to TabDocument is probably negligible, but if you are looking to optimize, storing it into an intermediate variable is a step towards positive.

But why you really should do it, is for code readability.


It would certainly enhance readability.

As to speed and responsiveness, the only way it would terribly improve anything is if the ActiveDocument or version properties perform many calculations in order to obtain their return value.


You should use a readonly variable. It would be faster and would help make your code more readable.

We should as much as possible avoid writing twice the same thing.

Hope this helps


ActiveDocument and String will both be one level of indirection away. So, they will both take the same amount of time.

You will add the extra overhead of creating a 'String' object with this, though, too.

But all of this should be incredibly negligible in the grand scheme of things. If you are looking to improve your speed you should look elsewhere!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜