Originally, this article was just a question I asked on Baidu, but because Baidu knew it was so disgusting that no one gave a proper answer for a long time and then wanted to answer the question, I found that the number of words was so limited, so I moved the problem here.
The problem involves creating objects twice:
1. Reflection creates an object;
2. Use reflection to create objects;
The second-level object can be created in two ways:
A) create an object using the new method
B) create objects through reflection
What is the performance difference between creating an object in the new method at the second level and directly using the new method (that is, skipping the first reflection operation?
I have never been able to wait until the answer I need is written by myself.CodeAfter the test, the test method is still executed in the form of 1 W, 10 W, 100 W, 1000 W, and 1 WW, and then the execution time is compared.
Last found:
1,A new object is the fastest;
2. Create an objectfactory through reflection, and then use objectfactory to create an object at the same speed as the new object;
3. The efficiency of direct reflection is the lowest.
As for why, I also give a simple personal opinion here:
1. In fact, the first time new was slow, but after the first new operation (DLL information related to this object was loaded into the memory) all new operations are the fastest, because each operation is read from the cache;
2. The reflection creates an object. Every time you read the DLL file, you can parse the DLL file and create an object. Here, the parsing means reflection.
3. First, reflect an objectfactory object and then create an object using the objectfactory method. The effect is similar to that of the first one, because when the objectfactory is created, load the information about the object to be created into the memory, read the DLL information from the memory, and create the object.
The previous test result image:
ASP. NET development technology exchange group:67511751(Recruitment ...)