直接从控件中读取字符串而不是将其移动到变量中是否有任何显着的好处?
sqlInsertFrame.Parameters.AddWithValue("@UserName", txtUserName.txt);
鉴于上面的代码...如果我不需要将文本框数据移动到字符串变量中,最好直接从控件读取数据吗?
就性能而言,最明智的做法是不创建任何不必要的变量,这些变量在不需要时会占用内存。或者这种情况在技术上是正确的,但由于相关数据的大小而无法产生任何现实世界的结果。
请原谅,我知道这是一个非常基本的问题。
sqlInsertFrame.Parameters.AddWithValue("@UserName", txtUserName.txt);
Given the code above...if I don't have any need to move the textbox data into a string variable, is it best to read the data directly from the control?
In terms of performance, it would seem smartest to not create any unnecessary variables which use up memory if its not needed. Or is this a situation where its technically true but doesn't yield any real world results due to the size of the data in question.
Forgive me, I know this is a very basic question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有验证?通常我会缓存字符串并将其传递给业务层进行验证,如果验证成功,则将缓存的字符串保存到数据库中。从窗口再次获取字符串比从缓存中读取值要慢。
No validation? Usually I cache the string and pass it to the business layer for validation, if the validation succeeds, then I save the cached string to the database. It is slower to get the string again from the Window than read the value off my cache.
实际上这根本不是一个非常基本的问题。就性能而言,如果不进行测量就很难说。在这种情况下,内部作用域中的局部变量很可能被智能编译器完全优化。它可能只存在于CPU寄存器中。
引入局部变量可以使你的代码更具可读性,如果可以的话,那就这么做吧!
另请参阅...局部变量赋值以避免多次转换并且,特别是接受的答案。
Actually it's not a very basic question at all. As far as performance goes it's hard to say without measuring. In this case, a local variable in an inner scope may well get optimized completely out of the picture by a smart compiler. It may exist only in the CPU registers.
Introducing a local variable can make your code much more readable, if it does, do it!
See also ... Local variable assignment to avoid multiple casts and, in particular, the accepted answer.
永远不要缓存您可以计算的内容。你的推理是合理的。
Never cache what you can compute. Your reasoning is sound.