I want to write data- the time it takes for a function to run- to a .txt file so that I can take it to Excel and make a spreadsheet (it's possible to make log-log and semi-log graphs in Excel).
Here's what I have so far: timing.java
public static void main(String args[]) {
Stopwatch timeMe = new Stopwatch();
...
try
{
File logFile = new File("log.txt");
FileWriter fw = new FileWriter(logFile2, true);
FileWriter fw2 = new FileWriter(logFile, true);
BufferedWriter bw = new BufferedWriter(fw);
// if file doesnt exists, then create it
if (!logFile.exists()) {
logFile.createNewFile();
System.out.println("doesn't exist");
}
...
for (int i = 1000; i <= 256000; i+=1000)
{
int[] randomArray = randomarr(i);
fw.write(String.valueOf(i));
fw.write(String.format("%n"));
timeMe.start();
pluto(randomArray);
timeMe.stop();
fw.write(timeMe.toString());
fw.write(String.format("%n"));
}
}
catch (IOException e)
{
e.printStackTrace();
}
...
}
Stopwatch.java
/**
A class to measure time elapsed.
*/
public class Stopwatch
{
private long startTime;
private long stopTime;
public static final double NANOS_PER_SEC = 1000000000.0;
/**
start the stop watch.
*/
public void start()
{ System.gc();
startTime = System.nanoTime();
}
/**
stop the stop watch.
*/
public void stop()
{ stopTime = System.nanoTime(); }
/**
elapsed time in secods.
@return the time recorded on the stopwatch in seconds
*/
public double time()
{ return (stopTime - startTime) / NANOS_PER_SEC; }
public String toString()
{ return "elapsed time: " + time() + " seconds."; }
/**
elapsed time in nanoseconds.
@return the time recorded on the stopwatch in nanoseconds
*/
public long timeInNanoseconds()
{ return (stopTime - startTime); }
}
The problem is, when I change anything in Stopwatch, i.e. remove the "elapsed time: " and " seconds." in the .toString() method to isolate the numbers, nothing will print to the file. In other words, the .txt file is empty. When I run it using the original Stopwatch.java file, everything is printed to the log.txt file, but I don't want this text.