If you need to collect results from hundreds or thousands of experimental runs of an algorithm, and you want it to be something other than the most tedious exercise known to man, automate it. Don’t run however many thousand experiments you need by hand! Your algorithms are code, right? And we can call code from within other code, right? So make a new program that automatically generates test data and uses your code directly to run the experiments. This is easiest if your algorithms are organized into one or more classes or modules so they can be easily imported into a separate experiment program.

There are, as with anything on the computer, many ways to get data from your program (such as runtimes) and collect them in one place. Here are some suggestions on how to do this efficiently.

Tip 1: Spreadsheet Programs Read CSV

CSV stands for comma-separated values. It is a simple text format that lets one encode data into rows and columns (just what spreadsheets want) using plain text. For example, the following lines in CSV format contain three rows with four columns each:

n,min,avg,max
8,0.04,0.3,1.3
16,0.09,0.56,2.22

Put that in a file, save it with a filename that ends in “.csv”, and then any spreadsheet program (LibreOffice Calc, Excel, etc.) can read it in directly.

Tip 2: Your Program can Output CSV

System.out.println(n + "," + min + "," + avg + "," + max);

There. Your program just printed out a single row of a CSV file, using data from variables named n, min, avg, and max.

Tip 3: You Can Redirect Your Program’s Output to a File

java MyProgram argument1 argument2 argument3 > output.txt

This redirects the output of your program (normally printed to the terminal) and saves it in a file called output.txt. The general pattern to run a command and store it in a particular file is:

command > file

This will not check to see if file already exists! It will simply overwrite whatever is there, if anything. Be careful, and don’t overwrite data you want to keep.

This is useful. Remember this. I use it several times a day.

It’s a computer. Let it do the hard work.