Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SetExercise;
- import java.io.*;
- import java.util.*;
- public class SetBenchmark {
- public static void main(String[] args) {
- String path = "war-and-peace.txt";
- HashSet<String> cache = new HashSet<>();
- // To cache the file so that Benchmarks should be about Equal.
- fileToSet(path, cache);
- HashSet<String> hs = new HashSet<>();
- TreeSet<String> ts = new TreeSet<>();
- long start = 0;
- long end = 0;
- for(int i = 0; i < 5; i++) {
- start += System.nanoTime();
- fileToSet(path,hs);
- end += System.nanoTime();
- hs = new HashSet<>();
- }
- start = start / 5;
- end = end / 5;
- long elapsed = end - start;
- System.out.println("Total time HashSet (ns): " + elapsed);
- start = 0;
- end = 0;
- elapsed = 0;
- for(int i = 0; i < 5; i++) {
- start += System.nanoTime();
- fileToSet(path, ts);
- end += System.nanoTime();
- ts = new TreeSet<>();
- }
- start = start / 5;
- end = end / 5;
- elapsed = end - start;
- System.out.println("Total time TreeSet (ns): " + elapsed);
- }
- static void fileToSet(String path, Set<String> set) {
- try {
- try (BufferedReader in = new BufferedReader(new FileReader(path))) {
- String line;
- while((line = in.readLine()) != null) {
- set.add(line);
- }
- }
- } catch(FileNotFoundException fnfe) {
- System.out.println(fnfe.getMessage());
- } catch(IOException ioe) {
- System.out.println(ioe.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement