Performance Tests on basic operations such as object creation in Java programs

Source: Internet
Author: User
In a Java program, you need to create an object, print the output to the screen, and start the thread. How long does it take? To solve this basic problem, we did the following tests: Test the hardware environment:Lenovo yangtian E3100, AMD Sempron 64-bit 2600 + (about 1.6 Ghz), M memory, 80 GB hard drive
Test software environment:Eclipse3.2.2 + JDK5.0, run directly in Eclipse Test code:Public class TestCreatObject ...{
 
Public static void main (String [] args )...{
Long start = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++ )...{
String str = new String ("" + I );
}
Long end1 = System. currentTimeMillis ();

For (int I = 0; I <10000; I ++)
System. out. println (I );

Long end2 = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++ )...{
TestThread tt = new TestThread ();
Tt. start ();
}
System. out. println ("create object time:" + (end1-start ));
System. out. println ("print screen time:" + (end2-end1 ));
System. out. println ("start thread time:" + (System. currentTimeMillis ()-end2 ));
}
 


}
Class TestThread extends Thread ...{
Public void run ()...{
// Try {
// Sleep (1 );
//} Catch (InterruptedException e ){
// E. printStackTrace ();
//}
}
}

Test results:Create object time: 32
Print screen time: 218
Start thread time: 1844
What is the relationship between the thread running time and startup speed during thread testing? ? The original thread did not execute anything, and now the blocked code is opened. , Respectively set the sleep () time from 1-> 10-> 100- "1000-" 10000, the result is as follows: 1-"start thread time: 211010-" start thread time: 2297100-"start thread time: 25941000-" start thread time: 567210000-"start thread time: 49875 Result Analysis:1. in Java programs, the time it takes to create an object is negligible. it takes a certain amount of time to print the output to the screen Because I/O operations are involved. It takes about 0.0218 ms each time. The system performance requirement is not very high and will not affect the performance of the program. it takes a lot of time to start a large number of concurrent threads. Even though the thread runs for a long time, the time spent is also rising. , It seems that multi-threaded processing is not the more threads, the better, you need to have a proper degree to maximize the process When the thread running time is longer, it may cause memory out-of-bounds exceptions.

 

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.