I need help filling out the milestones at the bottom of this code:
Milestone 2: Write methods to time the ArrayList and LinkedList .get() methods and print the
table.
Milestone 3: Write methods to time the ArrayList .add(0, element) and LinkedList .addFirst
methods and print the table.
Milestone 4: Write methods to time the .contains method for each of the three data structures
(two lists and HashSet) and print the table
CODE:
package list.timing;
import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.HashSet;
public class Main
{
public static final int iters = 100;
public static final int[] sizes = {10, 50, 100, 500, 1000, 10000};
// ---
// Example of how to time functions that change the size of the ADT
public static float timeAdd(List<Integer> startlist)
{
long total = 0;
long start, end;
for (int i=0; i<iters; i++)
{
start = System.nanoTime();
startlist.add(0);
end = System.nanoTime();
total += (end - start);
startlist.remove(0);
}
return total / (float)iters;
}
public static float timeAdd(HashSet<Integer> startset)
{
long total = 0;
long start, end;
for (int i=0; i<iters; i++)
{
start = System.nanoTime();
startset.add(-1);
end = System.nanoTime();
total += (end - start);
startset.remove(-1);
}
return total / (float)iters;
}
//---
// Example of how to time methods that don't change the size of the ADT
public static float timeSet(List<Integer> startlist)
{
long total = 0;
long start, end;
start = System.nanoTime();
int idx = startlist.size() / 2;
for (int i=0; i<iters; i++)
{
startlist.set(idx, 0);
}
end = System.nanoTime();
total = end - start;
return total / (float) iters;
}
//---
public static void fillToSize(List<Integer> list, int size)
{
list.clear();
for (int i=0; i<size; i++)
list.add(i);
}
public static void fillToSize(HashSet<Integer> hs, int size)
{
hs.clear();
for (int i=0; i<size; i++)
hs.add(i);
}
public static void printTable(String title, List<Float> lltimes, List<Float> altimes, List<Float>
hstimes)
{
System.out.println("--- " + title + "---");
System.out.printf("%4s\t","");
for(int size: sizes)
System.out.printf("%10d\t", size);
System.out.println();
System.out.printf("%4s\t", "AL");
for(Float time: altimes)
{
System.out.printf("%10.2f\t", time.floatValue());
}
System.out.println();
System.out.printf("%4s\t", "LL");
for(Float time: lltimes)
{
System.out.printf("%10.2f\t", time.floatValue());
}
System.out.println();
System.out.printf("%4s\t", "HS");
for(Float time: hstimes)
{
System.out.printf("%10.2f\t", time.floatValue());
}
System.out.println();
System.out.println();
}
public static void printTable(String title, List<Float> lltimes, List<Float> altimes)
{
System.out.println("--- " + title + "---");
System.out.printf("%4s\t","");
for(int size: sizes)
System.out.printf("%10d\t", size);
System.out.println();
System.out.printf("%4s\t", "AL");
for(Float time: altimes)
{
System.out.printf("%10.2f\t", time.floatValue());
}
System.out.println();
System.out.printf("%4s\t", "LL");
for(Flo.
I need help filling out the milestones at the bottom of this code- Mil.pdf
1. I need help filling out the milestones at the bottom of this code:
Milestone 2: Write methods to time the ArrayList and LinkedList .get() methods and print the
table.
Milestone 3: Write methods to time the ArrayList .add(0, element) and LinkedList .addFirst
methods and print the table.
Milestone 4: Write methods to time the .contains method for each of the three data structures
(two lists and HashSet) and print the table
CODE:
package list.timing;
import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.HashSet;
public class Main
{
public static final int iters = 100;
public static final int[] sizes = {10, 50, 100, 500, 1000, 10000};
// ---
// Example of how to time functions that change the size of the ADT
public static float timeAdd(List<Integer> startlist)
{
long total = 0;
long start, end;
for (int i=0; i<iters; i++)
{
start = System.nanoTime();
startlist.add(0);
end = System.nanoTime();
total += (end - start);
startlist.remove(0);
}
return total / (float)iters;
}
public static float timeAdd(HashSet<Integer> startset)
{
long total = 0;
long start, end;
for (int i=0; i<iters; i++)
{
start = System.nanoTime();
2. startset.add(-1);
end = System.nanoTime();
total += (end - start);
startset.remove(-1);
}
return total / (float)iters;
}
//---
// Example of how to time methods that don't change the size of the ADT
public static float timeSet(List<Integer> startlist)
{
long total = 0;
long start, end;
start = System.nanoTime();
int idx = startlist.size() / 2;
for (int i=0; i<iters; i++)
{
startlist.set(idx, 0);
}
end = System.nanoTime();
total = end - start;
return total / (float) iters;
}
//---
public static void fillToSize(List<Integer> list, int size)
{
list.clear();
for (int i=0; i<size; i++)
list.add(i);
}
public static void fillToSize(HashSet<Integer> hs, int size)
{
hs.clear();
for (int i=0; i<size; i++)
hs.add(i);
}
public static void printTable(String title, List<Float> lltimes, List<Float> altimes, List<Float>
hstimes)
{
System.out.println("--- " + title + "---");
System.out.printf("%4st","");
for(int size: sizes)
System.out.printf("%10dt", size);
4. /**
* @param args the command line arguments
*/
public static void main(String[] args)
{
LinkedList<Integer> ll = new LinkedList<>();
ArrayList<Integer> al = new ArrayList<>();
HashSet<Integer> hs = new HashSet<>();
ArrayList<Float> lltimes = new ArrayList<>(); // time for linked list ops
ArrayList<Float> altimes = new ArrayList<>(); // time for array list ops
ArrayList<Float> hstimes = new ArrayList<>(); // time for hash set ops
// This code times the .add method, and adds the result to the appropriate list of times.
for (int size: sizes)
{
fillToSize(ll, size);
lltimes.add(timeAdd(ll));
fillToSize(al, size);
altimes.add(timeAdd(al));
fillToSize(hs, size);
hstimes.add(timeAdd(hs));
}
// print the table for the .add method
printTable("Add (.add)", lltimes, altimes, hstimes);
//Make sure to clear all of the times arraylists so you don't have extra times being tacked on.
lltimes.clear();
altimes.clear();
hstimes.clear();
for (int size: sizes)
{
fillToSize(ll, size);
lltimes.add(timeSet(ll));
fillToSize(al, size);
altimes.add(timeSet(al));
}
printTable("Set index in middle (.set)", lltimes, altimes);
// TODO: Milestone 2: Time ArrayList and LinkedList .get() method
// You will need to write timeGet for linked list and array list
// remember to clear your times arrays.
5. //TODO: Milestone 3: Time .addFirst for LinkedList and .add(0, element) for ArrayList
// You will need to write the appropriate timing methods as above
//TODO: MIlestone 4: Time .contains() for LinkedList, ArrayList, and HashSet
// You will need to write the appropriate timing methods as above
}
}