HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

HashSet and TreeSet

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andhashsettreeset

Problem

I got some homework in which I had to take the novel War and Peace and put it into a HashSet and TreeSet respectively. I had to time it, to check differences and my question is whether my implementation is good or not.
If the way I calculate time is even accurate. I am using

System.currentMillis()


but I was debating with myself whether

System.nanoTime()


would be a better choice. I might just have misunderstood something about the assignment since this just seems way too easy to be the actual solution.

Just to clarify: The code works. I am questioning the efficiency of my implementation.

```
package SetExercise;

import java.io.*;
import java.util.*;

public class FileToSet {

public static void main(String[] args) {
HashSet hs = new HashSet<>();
TreeSet ts = new TreeSet<>();
long start = System.currentTimeMillis();
fileToHashSet("war-and-peace.txt", hs);
long end = System.currentTimeMillis();
long elapsed = end - start;
System.out.println("Total time HashSet (ms): " + elapsed);
start = System.currentTimeMillis();
fileToTreeSet("war-and-peace.txt", ts);
end = System.currentTimeMillis();
elapsed = end - start;
System.out.println("Total time TreeSet (ms): " + elapsed);
}

static void fileToHashSet(String path, HashSet set) {
try {
BufferedReader in = new BufferedReader(new FileReader(path));
while(in.readLine() != null) {
String line = in.readLine();
set.add(line);
}
in.close();
} catch(FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
}
}

static void fileToTreeSet(String path, TreeSet set) {
try {
BufferedReader in = new BufferedReader(new FileReader(path));
while(in.readLine() != nul

Solution

To measure the time taken, you should use System.nanoTime. That should only be used for "stopwatch" type operations - never for taking the "current system time" in a wall-clock way. Conversely, System.currentTimeMillis should not be used for "stopwatch" type operations, as it can be affected by system clock changes etc.

From the docs for nanoTime:


This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

Note that the important point isn't the difference in granularity here - it's the difference in purpose. They could both be returning milliseconds, and it would still make sense to have two different calls.

Context

StackExchange Code Review Q#15505, answer score: 28

Revisions (0)

No revisions yet.