-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimingTestDemo.java
More file actions
30 lines (27 loc) · 927 Bytes
/
TimingTestDemo.java
File metadata and controls
30 lines (27 loc) · 927 Bytes
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
package bearmaps;
import edu.princeton.cs.algs4.Stopwatch;
/**
* Created by hug. Demonstrates how you can use either
* System.currentTimeMillis or the Princeton Stopwatch
* class to time code.
*/
public class TimingTestDemo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
int sum = 0;
for (int i = 0; i < 100000; i += 1) {
for (int j = 0; j < 10000; j += 1) {
sum = sum + i + j;
}
}
long end = System.currentTimeMillis();
System.out.println("Total time elapsed: " + (end - start)/1000.0 + " seconds.");
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 100000; i += 1) {
for (int j = 0; j < 10000; j += 1) {
sum = sum + i + j;
}
}
System.out.println("Total time elapsed: " + sw.elapsedTime() + " seconds.");
}
}