简体   繁体   English

使用Factory Pattern和If-else或switch时.NET JIT的区别

[英].NET JIT difference while using Factory Pattern and If-else or switch

I was just wandering, if I was to have a one method that handles 100 different cases based upon an enum let's say (assuming each case has on average 5 lines of code), would that actually impact performance ? 我只是在徘徊,如果我有一种方法可以基于一个枚举(假设每个案例平均有5行代码)来处理100个不同的案例,那是否会对性能产生实际影响? How about instead of having all the code in one method, the Factory or Strategy pattern would to be used ? 如何使用Factory或Strategy模式而不是将所有代码都用一种方法呢?

The JIT only compiles the code which is actually needed at that point. JIT仅编译当时实际需要的代码。 So I guess it will compile the hole method of 100 cases right? 那么我想它将编译100个案例的钻孔方法吧? It wouldn't actually know what part of that method is needed correct ? 它实际上不知道该方法的哪一部分需要正确吗? but if I was to split that method it will actually compile what it is needed right?. 但是如果我要拆分该方法,它将实际上编译所需的内容,对吗? For example having an actions drop-down (list of 100 Car Brands) 例如,有一个动作下拉菜单(100个汽车品牌的列表)

How would that compare to each other in terms of performance? 在性能方面,这又如何比较?

Thanks. 谢谢。

I am afraid you are missing the essential thing that Jit does. 恐怕您错过了Jit所做的重要工作。 It compiles cil which is CPU and platform independent into machine and platform specific machine code. 它将与CPU和平台无关的cil编译为机器和平台特定的机器代码。

The JIT only compiles the code which is actually needed at that point. JIT仅编译当时实际需要的代码。

Yes, the very first time your method is called then the Jit compiles it, hence the name Just-In-Time compiler . 是的,第一次调用您的方法,然后Jit对其进行编译,因此命名为Just-In-Time Compiler

So I guess it will compile the hole method of 100 cases right? 那么我想它将编译100个案例的钻孔方法吧?

Yes. 是。

It wouldn't actually know what part of that method is needed correct ? 它实际上不知道该方法的哪一部分需要正确吗? but if I was to split that method it will actually compile what it is needed right? 但是如果我要拆分该方法,它将实际上编译所需的内容,对吗?

Now here the confusion comes, yes it will compile the whole method, but notice this will be only once per application startup. 现在,这里出现了混乱,是的,它将编译整个方法,但是请注意,每次应用程序启动仅一次。 Since, Jit compiles at runtime we can say that there will be some negligible performance impact first time method was called, one can argue that splitting this method into multiple methods will require multiple Jit compilations. 由于Jit在运行时进行编译,因此可以说第一次调用该方法对性能的影响可以忽略不计 ,因此可以说将此方法拆分为多个方法将需要多次Jit编译。

With that being said, if you are working on some very high demand for performance application and you are worrying about Jit being drawback you can always use ngen and compile native images before application start and then CLR can use those images to spin up the process for you and this will remove Jit compilation as it was basically done before application was started. 话虽如此,如果您正在处理一些对性能应用程序非常高的需求,并且担心Jit的缺点,则可以始终使用ngen并在应用程序启动之前编译本机映像,然后CLR可以使用这些映像来加速进程。您和这将删除Jit编译,因为它基本上是在启动应用程序之前完成的。 I can see how this might be useful for applications caring about cold startups such as applications hosted on function as a service platforms but thats as far as I can go with the real world examples. 我可以看到这对于关心冷启动的应用程序(如托管在服务平台上的应用程序)如何有用,但就我在现实世界中的例子而言,这还可以。

And in the end, 最后,

How would that compare to each other in terms of performance? 在性能方面,这又如何比较?

to be honest I wouldn't be worried ever about Jit compilation impact, I'd rather focus on the data structures I am using or the domain specific logic I have in order to improve my performance. 老实说,我永远不会担心Jit编译的影响,我宁愿专注于我正在使用的数据结构或我拥有的特定领域逻辑,以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM