Recommendation 60: Performance considerations, arrays are preferred

Source: Internet
Author: User
Package com.company.section1;


Import java.util.ArrayList;
Import java.util.List;




public class Client {
Number of cycles performed by test performance
private static final int loop_num = 100000;
public static void Main (string[] args) {
Int[] score = new INT[100];
Initializing arrays and lists
list<integer> list = new arraylist<integer> (100);
for (int i=0;i<score.length;i++) {
Score[i] = i;
List.add (i);
}
Test Array Performance
Long begin = System.currenttimemillis ();
for (int i=0;i<loop_num;i++) {
SUM (score);
}
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN (array) Execution time: + (End-begin) + "MS");

Test List Execution Time
for (int i=0;i<loop_num;i++) {
sum (list);
}
SYSTEM.OUT.PRINTLN (list) Execution time: + (System.currenttimemillis ()-end) + "MS");


}
Sum of pairs of groups
public static int sum (int[] datas) {
int sum = 0;
for (int i=0;i<datas.length;i++) {
Sum +=datas[i];
}
return sum;
}

Sum the list
public static int sum (list<integer> datas) {
int sum = 0;
for (int i=0;i<datas.size (); i++) {
Sum +=datas.get (i);
}
return sum;
}
}

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.