通用类型和性能 [英] Generic type and performance
问题描述
我想知道如果在整个程序中使用泛型将会对应用程序产生任何(显着)性能下降的影响。我使用了很多集合对象(列表,集合,映射)并为应用程序提供了更好的灵活性(此应用程序必须集成到其他应用程序中)我只使用泛型类型而不是参数化它们。这是一种常见的做法还是有人有建议/建议?是否会有显着的性能下降?
在此先感谢,
<编译器会使用这些信息,因为泛型实际上就是一种编译时候的技巧。
编译器使用这些信息拒绝某些类型不安全的做法,否则将出现在运行时,如果不使用泛型。在运行期间,只使用原始类(即泛型参数的上限),因此使用泛型时性能与不 相同,只需自己投射类。I'm wondering if using generic types through out the program will have any (significant)degrading performance effect on the application. I use a lot of collection objects(lists, sets, maps) and to give better flexibility to the application(this application has to be integrated into other application) I used only generic types instead of parameterizing them. Is this a common practice or does anyone have a suggestion/advice? Will there be any significant performance degrade?
Thanks in advance,
There will be no performance degradation at all, because generics are effectively a "compile-time trick".
The compiler uses this information to reject certain type-unsafe practices that would otherwise appear at runtime, if not using generics. During runtime only the raw classes (i.e. the upper bound of the generic parameters) are used, so the performance will be identical to not using generics, and just casting the classes yourself.
这篇关于通用类型和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!