Spring gets an instance and a new instance. Which one is faster?

Source: Internet
Author: User

The bean configured in spring is the default Singleton, so in the program, the speed of getting an instance must be faster than creating an instance, saving resources. During the actual test today, we found that a new object is much faster than a spring object. Next I added a singleton test to compare the test results. Copy the public static void main (String [] args) {ReceiveProcess receiveProcess; applicationContext appCt = new ClassPathXmlApplicationContext ("classpath: applicationContext. xml "); long begin = System. currentTimeMillis (); int num = 10000000; for (int I = 0; I <num; I ++) {receiveProcess = (ReceiveProcess) appCt. getBean ("textReceive");} System. out. println ("spring obtains the total instance time:" + (Sy Stem. currentTimeMillis ()-begin); long begin2 = System. currentTimeMillis (); ReceiveProcess re = null; for (int j = 0; j <num; j ++) {re = new TextReceiveProcess ();} System. out. println ("Total time the instance was created:" + (System. currentTimeMillis ()-begin2); ReceiveProcess reInstance = null; long begin3 = System. currentTimeMillis (); for (int d = 0; d <num; d ++) {reInstance = TextReceiveProcess. getInstance ();} System. out. println ("total time in a single case:" + (System. currentTimeMillis ()-begin3);} result of copying the code: Total instance time for spring: 5257 total time for instance creation: total time for 48 instances: 16 occasionally, this result is not very understandable. Sometimes the time for creating an object is the same as that for a single instance. Why? Total time for spring instances: 5258 total time for instance creation: 32 in a single case: 31

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.