-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimedComputation.java
51 lines (39 loc) · 1.73 KB
/
TimedComputation.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
package nadine;
/**
* This program performs some mathematical computations and displays the
* results. It also displays the value of the constant Math.PI. It then
* reports the number of seconds that the computer spent on this task.
*/
public class TimedComputation {
public static void main(String[] args) {
long startTime; //Starting time of program, in nanoseconds.
long endTime; // Time when computations are done, in nanoseconds.
startTime = System.nanoTime();
double width, height, hypotenuse; // sides of a triangle
width = 42.0;
height = 17.0;
hypotenuse = Math.sqrt( width*width + height*height );
System.out.print("A triangle with sides 42 and 17 has hypotenuse ");
System.out.println(hypotenuse);
System.out.println("\nMathematically, sin(x)*sin(x) + "
+ "cos(x)*cos(x) - 1 should be 0.");
System.out.println("Let’s check this for x = 100:");
System.out.print(" sin(100)*sin(100) + cos(100)*cos(100) - 1 is: ");
System.out.println( Math.sin(100)*Math.sin(100)
+ Math.cos(100)*Math.cos(100) - 1 );
System.out.println("(There can be round-off errors when"
+ " computing with real numbers!)");
System.out.print("\nHere is a random number: ");
System.out.println( Math.random() );
System.out.print("\nThe value of Math.PI is ");
System.out.println( Math.PI );
endTime =System.nanoTime();
height = endTime - startTime;
width = height / 1000000000.0;
System.out.print("/nRun time in nanoseconds was: ");
System.out.println(height);
System.out.println("(This is probably not perfectly accurate!");
System.out.print("/nRun time in seconds was: ");
System.out.println(width);
} // end main()
} // end class TimedComputation