避免在 DbParameter.Value 中装箱?
我正在使用 ADO.NET 来通信一些数据库,并寻找一种在将 DbParameter.Value 属性设置为值类型时避免装箱的方法。
有没有办法避免 DbParameter.Value 中的装箱?
谢谢。
I'm using ADO.NET to communicate some db, and searching for a way to avoid boxing when setting the DbParameter.Value property to value-type.
Is there a way to avoid boxing in DbParameter.Value?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要避免它?在整个数据访问层的上下文中,装箱的性能成本可能几乎为零。仅在线路上序列化参数值就可能是装箱成本的 100 倍。您是否发现拳击造成的性能问题?
Why do you want to avoid it? The performance cost of boxing is probably next to nothing in the context of the overall data access layer. Serializing the parameter value on the wire alone is probably 100 times the cost of boxing. Are you seeing a performance problem attributable to boxing?
我不这么认为。如果您检查 Reflector 中 SqlParameter 类的 Value 属性,您将看到存储值的内部字段也是对象类型。它没有地方可以存储整数或浮点数或任何其他方式。
I don't think so. If you examine the Value property of the SqlParameter class in Reflector, you will see that the internal field where the value is stored is also of type object. There is no place for it to store ints or floats or whatever any other way.
您无法避免使用装箱值,但可以避免实际装箱。或者更确切地说,您可以更改拳击发生的方式和时间。
您可以准备在参数中使用的装箱值,例如:
只要保留数组,装箱值就可以反复使用而不会导致更多装箱,并且直到数组不再使用时才会收集它们。
拳击是否真的会导致任何性能问题值得怀疑,但现在您至少有办法进行一些实际测试。
You can't avoid using boxed values, but you can avoid the actual boxing. Or rather, you can change how and when the boxing occurs.
You can prepare boxed values to use in parameters, for example:
As long as you keep the array, the boxed values can be used over and over without causing any more boxing, and they will not be collected until the array goes out of use.
It's doubtful that boxing is really causing any performance problems, but now you have a way to do some real testing at least.