Performance between Sets and List
来源:互联网 发布:圣马可吉他怎么样知乎 编辑:程序博客网 时间:2024/06/05 18:18
The sets and list are offen used to store the processing data, but which one is more efficient and performance better, here below is the example for testing which one is better.
Example:
package com.basic.set;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
public class SetListPerformanceTest {
private static final int N = 50000;
public static void main(String[] args) {
//Add numbers 0,1,2,3....N-1 to the array list
List<Integer> list = new ArrayList<Integer>();
for (int i=0; i<N; i++){
list.add(i);
}
//shuffle: randomly make the list to be disorganize
Collections.shuffle(list);//shuffle the array list
//create a hash set, and test its performance
Collection<Integer> set1 = new HashSet<Integer>();
System.out.println("Member test time for hash set is " + getTestTime(set1) + "milliseconds");
System.out.println("Remove element time for hash set is " + getRemoveTime(set1) + "milliseconds");
//create a linked hase set, and test its performance
Collection<Integer> set2 = new LinkedHashSet<Integer>(list);
System.out.println("Member test time for linked hash set is " + getTestTime(set2) + "millseconds");
System.out.println("Remove element time for linked hash set is " + getRemoveTime(set2) + "milliseconds");
//create a linked hase set, and test its performance
Collection<Integer> set3 = new LinkedHashSet<Integer>(list);
System.out.println("Member test time for tree set is " + getTestTime(set3) + "millseconds");
System.out.println("Remove element time for tree set is " + getRemoveTime(set3) + "milliseconds");
//create an array list , and test its performance
Collection<Integer> list1 = new ArrayList<Integer>(list);
System.out.println("Member test time for array list is " + getTestTime(list1) + "millseconds");
System.out.println("Remove element time for array list is " + getRemoveTime(list1) + "milliseconds");
//create an array list , and test its performance
Collection<Integer> list2 = new LinkedList<Integer>(list);
System.out.println("Member test time for linked list is " + getTestTime(list2) + "millseconds");
System.out.println("Remove element time for linked list is " + getRemoveTime(list2) + "milliseconds");
}
private static long getRemoveTime(Collection<Integer> c) {
long startTime = System.currentTimeMillis();
for (int i=0; i < N; i++)
c.remove(i);
return System.currentTimeMillis() - startTime ;
}
private static long getTestTime(Collection<Integer> c) {
long startTime = System.currentTimeMillis();
for (int i=0; i < N; i++) {
c.contains(Math.random() * 2 * N);
}
return System.currentTimeMillis() - startTime;
}
}
Output Result:
Member test time for hash set is 16milliseconds
Remove element time for hash set is 8milliseconds
Member test time for linked hash set is 12millseconds
Remove element time for linked hash set is 13milliseconds
Member test time for tree set is 8millseconds
Remove element time for tree set is 9milliseconds
Member test time for array list is 29096millseconds
Remove element time for array list is 9513milliseconds
Member test time for linked list is 34799millseconds
Remove element time for linked list is 7601milliseconds
- Performance between Sets and List
- Differences between Thumb and ARM instruction sets
- difference between slist and list.
- Difference Between List and Tuples
- HackerRank Between Two Sets
- reverse list between m and n
- Is there any difference between List x; and List x();?
- What is the difference between Simple, Advanced and Parameter data sets?
- TCP Performance problems caused by interaction between Nagle's Algorithm and Delayed ACK
- Difference Between Performance Testing, Load Testing and Stress Testing – With Examples
- Difference between List and Set in Java Collection
- Difference between List and Set in Java Collection
- Difference between Set, List and Map in Java - Interview question
- differences between List and Set interface in point format
- Difference between List and Set in Java Collection
- Difference between List View and DataGrid in WPF
- between...and
- between ..... and ..........
- window 查看IP地址、网管、子网掩护、DNS等信息
- 设置VMWARE通过桥接方式使用主机无线网卡上网
- 函数指针
- Foundation -- OC集合 --NSSet
- 基于asio实现pop3的几个指令
- Performance between Sets and List
- 黑马程序员——反射
- P51第15题
- 偏底层(深入)的一些概念整理
- PC用Kinect玩水果忍者核心代码
- __asm__ __volatile__
- Foundation -- OC集合 --NSDictionary
- CF 526C Om Nom and Candies
- LinkedList