Results 1 to 4 of 4
- 05-18-2012, 01:01 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
comparing time performance: Array vs LinkedList vs ArrayList
My Professor asked us to compare the Time performance: Array vs LinkedList vs ArrayList.
we have to write following methods:
Add to front
Add to end
Iterate through n elements
delete first Element n times
I wrote the program but i don't know I have the feeling it is wrong.
the Output for n=100000Java Code:public class PerformanceTest { //variable declaration int n; //constructor public PerformanceTest(int number) { this.n = number; } //add Elements to front of ArrayLit and LinkedList //the list is a List of Objects //this method will write the Object obj //n times to the Front of List public void addToFrontList(List list) { //creates new Object to add. Object obj = new Object(); long start = System.currentTimeMillis(); //add Object at position 0, all elements will be //moved to the right for (int i = 0; i < this.n - 1; i++) { list.add(0, obj); } System.out.println(System.currentTimeMillis() - start); } //adds Elements to front of Array //this method will write the Object obj //n times to the Front of the Array. //All old Elements will be moved // to the end of the New Array public void addToFrontArray(Object[] tab) { //creates new object Object obj = new Object(); int vorIndex=tab.length; tab = Arrays.copyOf(tab, this.n + tab.length); int index = tab.length - 1; long start = System.currentTimeMillis(); //moves all elements to the right of the new Array for (int s = vorIndex; s >= 0; s--) { tab[index] = tab[s]; index--; } //Writes n times object to Front of Array for (int i = 0; i < n; i++) { tab[i] = obj; } System.out.println(System.currentTimeMillis() - start); } private void addToEndArray(Object[] tab) { //Creates new Object Object obj = new Object(); //creates new Array of objects + makes array bigger Object[] tabNew = Arrays.copyOf(tab, this.n + tab.length); long start = System.currentTimeMillis(); //writes Elements in Array for (int s = tab.length; s < tabNew.length; s++) { tabNew[s] = obj; } System.out.println(System.currentTimeMillis() - start); } private void addToEndList(List list) { //creates new Object to add. Object obj = new Object(); long start = System.currentTimeMillis(); //add Object at position 0, all elements will be //moved to the right for (int i = 0; i < this.n; i++) { list.add(obj); } System.out.println(System.currentTimeMillis() - start); } private void iterateArray(Object[] tab) { Object j; long start = System.currentTimeMillis(); for (int i = 0; i < this.n - 1; i++) { j = tab[i]; } System.out.println(System.currentTimeMillis() - start); } private void iterateArrayList(List list) { Object j; long start = System.currentTimeMillis(); for (int i = 0; i < this.n; i++) { j = list.get(i); } System.out.println(System.currentTimeMillis() - start); } private void deleteArray(Object[] tab) { long start = System.currentTimeMillis(); for (int i = 0; i < this.n; i++) { tab = Arrays.copyOfRange(tab, 1, tab.length); } System.out.println(System.currentTimeMillis() - start); } private void deleteArrayList(List list) { long start = System.currentTimeMillis(); for (int i = 0; i < this.n; i++) { list.remove(0); } System.out.println(System.currentTimeMillis() - start); } public static void main(String[] args) { PerformanceTest test = new PerformanceTest(100000); ArrayList<Object> list = new ArrayList<Object>(); LinkedList<Object> linkedList = new LinkedList<Object>(); Object[] array = new Object[0]; System.out.println("Performance addtoFrontArray: Array"); test.addToFrontArray(array); System.out.println("Performance addtoFrontList: ArrayList"); test.addToFrontList(list); System.out.println("Performance addtoFrontlist: LinkedList"); test.addToFrontList(linkedList); System.out.println("Performance addtoEndArray: Array"); test.addToEndArray(array); System.out.println("Performance addtoEndlist: ArrayList"); test.addToEndList(list); System.out.println("Performance addtoEndlist: LinkedList"); test.addToEndList(linkedList); } }
Performance addtoFrontArray: Array
15
Performance addtoFrontList: ArrayList
6927
Performance addtoFrontlist: LinkedList
31
Performance addtoEndArray: Array
0
Performance addtoEndlist: ArrayList
16
Performance addtoEndlist: LinkedList
16Last edited by ossa; 05-18-2012 at 02:11 PM.
- 05-18-2012, 02:09 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: comparing time performance: Array vs LinkedList vs ArrayList
Please use [code] tags [/code] when posting code.
Hardly anyone will bother reading that without them.
You also need to give us an idea of what you think is wrong.Please do not ask for code as refusal often offends.
- 05-18-2012, 02:12 PM #3
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: comparing time performance: Array vs LinkedList vs ArrayList
Hi
for example I don t understand why the time performance of AddToEndArray = 0
- 05-18-2012, 02:39 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: comparing time performance: Array vs LinkedList vs ArrayList
You're starting your timer after the bulk of the work has already been done.Java Code:Object[] tabNew = Arrays.copyOf(tab, this.n + tab.length); long start = System.currentTimeMillis();
So all you're timing is how long it takes to change the last value in the new array.
Which is going to be shorter than the timer can handle.
I would suggest timing somehting like this in a loop doing lots of additions, since most of what you're going to see on a single one is noise.Please do not ask for code as refusal often offends.
Similar Threads
-
differece between ArrayList and LinkedList
By srinivasmallabathula in forum Advanced JavaReplies: 3Last Post: 06-14-2011, 08:37 AM -
time comparing
By cucucur in forum Advanced JavaReplies: 7Last Post: 04-28-2011, 05:02 PM -
How can I copy values 'only' to LinkedList/ArrayList?
By smtwtfs in forum New To JavaReplies: 2Last Post: 04-28-2011, 11:25 AM -
ArrayList vs LinkedList
By rp181 in forum New To JavaReplies: 2Last Post: 02-27-2011, 02:08 AM -
Creating an ArrayList from an existing LinkedList
By Java Tip in forum Java TipReplies: 0Last Post: 12-05-2007, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks