Lab 1 - Chapter 4 The Efficiency of Algorithms 01/24/2023 In this lab, you will practice how to measure the running time of a section of code in Java. One approach is to use System.nanoTime() where the current time is stored as a long integer equals to the number of nanoseconds. By subtracting the starting time in nanoseconds from the ending time in nanoseconds, you get the run time in nanoseconds of a section of code. For example, suppose that AlgorithmA is the name of a method you wish to time. The following statements will compute the number of nanoseconds that AlgorithmA requires to execute: public static void main(String[] args) { int n1 = 10, n2 = 100, n3 = 1000, n4 long n1Time, n2Time, n3Time, n4Time; n1Time = AlgorithmA (nl); } public static long AlgorithmA (int n) { long startTime, endTime, elapsedTime; startTime = System.nanoTime (); int sum = 0; for (int i = 1; i <= n; i++) { sum sum + i; } 1. } endTime = System.nanoTime (); // Get current time in nanosecond elapsedTime = endTime- startTime; // Elapsed time calculation return elapsedTime; Algorithm A Write a Java program that implements three different algorithms below in Figure 4-1 and times them for various values of n (10, 100, 1000, 10000). The program should display a table of the run times of each algorithm for the values of n. Figure 4-1 sum = 0 for i=1 to n sum = sum + 1 Algorithm B sum = 0 for i=1 to n { 10000; for j = 1 to 1 // Get current time in nanosecond sum sum +1 Algorithm C sum = n(n+1) / 2 } Three algorithms for computing the sum 1+2+...+n for an integer n > 0 2. By midnight, Tuesday, Jan 24th, submit your Java source file and a screenshot of the execution result via 'Submit Lab Assignments' in the course Blackboard.
Lab 1 - Chapter 4 The Efficiency of Algorithms 01/24/2023 In this lab, you will practice how to measure the running time of a section of code in Java. One approach is to use System.nanoTime() where the current time is stored as a long integer equals to the number of nanoseconds. By subtracting the starting time in nanoseconds from the ending time in nanoseconds, you get the run time in nanoseconds of a section of code. For example, suppose that AlgorithmA is the name of a method you wish to time. The following statements will compute the number of nanoseconds that AlgorithmA requires to execute: public static void main(String[] args) { int n1 = 10, n2 = 100, n3 = 1000, n4 long n1Time, n2Time, n3Time, n4Time; n1Time = AlgorithmA (nl); } public static long AlgorithmA (int n) { long startTime, endTime, elapsedTime; startTime = System.nanoTime (); int sum = 0; for (int i = 1; i <= n; i++) { sum sum + i; } 1. } endTime = System.nanoTime (); // Get current time in nanosecond elapsedTime = endTime- startTime; // Elapsed time calculation return elapsedTime; Algorithm A Write a Java program that implements three different algorithms below in Figure 4-1 and times them for various values of n (10, 100, 1000, 10000). The program should display a table of the run times of each algorithm for the values of n. Figure 4-1 sum = 0 for i=1 to n sum = sum + 1 Algorithm B sum = 0 for i=1 to n { 10000; for j = 1 to 1 // Get current time in nanosecond sum sum +1 Algorithm C sum = n(n+1) / 2 } Three algorithms for computing the sum 1+2+...+n for an integer n > 0 2. By midnight, Tuesday, Jan 24th, submit your Java source file and a screenshot of the execution result via 'Submit Lab Assignments' in the course Blackboard.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education