-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentBenchmarking.java
100 lines (81 loc) · 3.06 KB
/
DocumentBenchmarking.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package document;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/** A class for timing the EfficientDocument and BasicDocument classes
*
* @author UC San Diego Intermediate Programming MOOC team
*
*/
public class DocumentBenchmarking {
public static void main(String [] args) {
// Run each test more than once to get bigger numbers and less noise.
// You can try playing around with this number.
int trials = 100;
// The text to test on
String textfile = "data/warAndPeace.txt";
// The amount of characters to increment each step
// You can play around with this
int increment = 20000;
// The number of steps to run.
// You can play around with this.
int numSteps = 20;
// THe number of characters to start with.
// You can play around with this.
int start = 50000;
// TODO: Fill in the rest of this method so that it runs two loops
// and prints out timing results as described in the assignment
// instructions.
for (int numToCheck = start; numToCheck < numSteps*increment + start;
numToCheck += increment)
{
// numToCheck holds the number of characters that you should read from the
// file to create both a BasicDocument and an EfficientDocument.
/* Each time through this loop you should:
* 1. Print out numToCheck followed by a tab (\t) (NOT a newline)
* 2. Read numToCheck characters from the file into a String
* Hint: use the helper method below.
* 3. Time a loop that runs trials times (trials is the variable above) that:
* a. Creates a BasicDocument
* b. Calls fleshScore on this document
* 4. Print out the time it took to complete the loop in step 3
* (on the same line as the first print statement) followed by a tab (\t)
* 5. Time a loop that runs trials times (trials is the variable above) that:
* a. Creates an EfficientDocument
* b. Calls fleshScore on this document
* 6. Print out the time it took to complete the loop in step 5
* (on the same line as the first print statement) followed by a newline (\n)
*/
}
}
/** Get a specified number of characters from a text file
*
* @param filename The file to read from
* @param numChars The number of characters to read
* @return The text string from the file with the appropriate number of characters
*/
public static String getStringFromFile(String filename, int numChars) {
StringBuffer s = new StringBuffer();
try {
FileInputStream inputFile= new FileInputStream(filename);
InputStreamReader inputStream = new InputStreamReader(inputFile);
BufferedReader bis = new BufferedReader(inputStream);
int val;
int count = 0;
while ((val = bis.read()) != -1 && count < numChars) {
s.append((char)val);
count++;
}
if (count < numChars) {
System.out.println("Warning: End of file reached at " + count + " characters.");
}
bis.close();
}
catch(Exception e)
{
System.out.println(e);
System.exit(0);
}
return s.toString();
}
}