473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

efficient of sets over lists

so im reading this chapter about sets lists and maps and one of the sections talks about the efficiency of sets over list and then it goes on to give code to demonstrate this very thing. however my results are different from the books. way different, can anyone explain to me why this is. my results say that lists are more efficient then sets. i have the output of running the code on my computer fallowed by the output given in the book.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class SetListPerformanceTest {
  4.     static final int N = 50000;
  5.  
  6.     public static void main(String[] args) {
  7.         // Add numbers 0, 1, 2, ... N-1 to the array list
  8.         List<Integer> list = new ArrayList<Integer>();
  9.         for (int i = 0; i < N; i++)
  10.             list.add(i);
  11.         Collections.shuffle(list); // shuffle the array list
  12.  
  13.         // Create a hash set,and test its performance
  14.         Collection<Integer> set1 = new HashSet<Integer>(list);
  15.         System.out.println("Member test time for hash set is " +
  16.                 getTestTime(set1) + " milliseconds");
  17.         System.out.println("Remove element time for hash set is " +
  18.                 getRemoveTime(set1) + " milliseconds");
  19.  
  20.  
  21.         // Create a linked hash set,and test its performance
  22.         Collection<Integer> set2 = new LinkedHashSet<Integer>(list);
  23.         System.out.println("Member test time for linked hash set is " +
  24.                 getTestTime(set2) + " milliseconds");
  25.         System.out.println("Remove element time for linked hash set is " +
  26.                 getRemoveTime(set2) + " milliseconds");
  27.  
  28.  
  29.         // Create a tree set,and test its performance
  30.         Collection<Integer> set3 = new TreeSet<Integer>(list);
  31.         System.out.println("Member test time for tree set is " +
  32.                 getTestTime(set3) + " milliseconds");
  33.         System.out.println("Remove element time for tree set is " +
  34.                 getRemoveTime(set3) + " milliseconds");
  35.  
  36.  
  37.         // Create a array list,and test its performance
  38.         Collection<Integer> list1 = new HashSet<Integer>(list);
  39.         System.out.println("Member test time for array list is " +
  40.                 getTestTime(list1) + " milliseconds");
  41.         System.out.println("Remove element time for array list is " +
  42.                 getRemoveTime(list1) + " milliseconds");
  43.  
  44.  
  45.         // Create a linked list,and test its performance
  46.         Collection<Integer> list2 = new HashSet<Integer>(list);
  47.         System.out.println("Member test time for linked list is " +
  48.                 getTestTime(list2) + " milliseconds");
  49.         System.out.println("Remove element time for linked list is " +
  50.                 getRemoveTime(list2) + " milliseconds");
  51.     }// END OF MAIN METHOD
  52.  
  53.     public static long getTestTime(Collection<Integer> c) {
  54.         long startTime = System.currentTimeMillis();
  55.  
  56.         // Test if a number is in the collection
  57.         for (int i = 0; i < N; i++)
  58.             c.contains((int)(Math.random() * 2 * N));
  59.  
  60.         return System.currentTimeMillis() - startTime;
  61.     }// END OF GET TEST TIME METHOD
  62.  
  63.     public static long getRemoveTime(Collection<Integer> c) {
  64.         long startTime = System.currentTimeMillis();
  65.  
  66.         for (int i = 0; i < N; i++)
  67.             c.remove(i);
  68.  
  69.         return System.currentTimeMillis() - startTime;
  70.     }// END OF GET REMOVE TIME METHOD
  71. }// END OF SET LIST PERFORMANCE TEST CLASS
  72.  
  73.  
my output >>>> books output
Member test time for hash set is 13 milliseconds >>> 20 millis
Remove element time for hash set is 9 milliseconds >> 27 millis
Member test time for linked hash set is 7 milliseconds >> 27
Remove element time for linked hash set is 12 milliseconds >>26
Member test time for tree set is 11 milliseconds >> 47
Remove element time for tree set is 22 milliseconds >> 34
Member test time for array list is 4 milliseconds >> 39802
Remove element time for array list is 1 milliseconds >> 16196
Member test time for linked list is 3 milliseconds >> 52197
Remove element time for linked list is 1 milliseconds >> 14870
Mar 3 '16 #1
1 1490
chaarmann
785 Expert 512MB
Sorry to say that but your tests are written in a way that comparison results are not meaningful. That is why your results are different from the ones in the book.

You should only test the find/remove of your collection. But you are also taking in account the time of additional commands.
Line 58 for example:
Expand|Select|Wrap|Line Numbers
  1.  c.contains((int)(Math.random() * 2 * N));
here you are measuring the total time for the contains method (the one you want to test) plus the time for the random method, for the multiplications and for the casting to an int (the ones you do not want to test).

Second example:
In Line 38 and 46 you are creating the same collection type (HashSet) that you are testing, but in the comment above you wrote it should be an array list and a linked list. Copy-and-paste error?

Third example:
If you measure the time to remove a member at a random position, then you also adding the time to find this position.

Fourth example: You are not testing the weakest and strongest path of the algorithm:
Let us say you have a list of 1000 items and you want to find/remove the item at position 500, then you would get following times:
find: hashset (accessing directly via hash value without comparing elements directly ) is much faster than array list or linked list where you have to go through the first 500 elements and compare each one with the current.
removal: linked list (only changing the 2 pointer of previous and next element) is much faster than array list or hashset, where you have to move all 500 elements down one position to fill the gap.

What I want to say is that you should always take into account how you use these sets.
If you search by random access, or you always search for one of the first elements, or in most cases for the last elements, makes a big difference in the times you measure.
Mar 7 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
5
by: Alan Little | last post by:
I need to merge and de-duplicate some lists, and I have some code which works but doesn't seem particularly elegant. I was wondering if somebody could point me at a cleaner way to do it. Here's...
42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
3
by: Chris S. | last post by:
>>> from sets import * >>> Set((1,2,3)) Set() >>> Set() Set() >>> Set((,)) Traceback (most recent call last): File "<input>", line 1, in ? File "sets.py", line 399, in __init__...
17
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way of doing it. >>> a= >>> b= >>> ...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
6
by: kpp9c | last post by:
I have a question... and ... whew ... i am gonna be honest, i haven't the slightest clue how to even start ... i am not sure if i used up all my good will here or can take a mulligan.. i love to...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
1
by: Girish Sahani | last post by:
hello ppl, Consider a list like . Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.