Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add for loops and display time for both System.out.pritnln and System.out.printf. The code is not displying the time it takes for both System.out.pritnln and System.out.printf.

Add for loops and display time for both System.out.pritnln and System.out.printf. The code is not displying the time it takes for both System.out.pritnln and System.out.printf. Any help will be greatly appreciated. This is in Java. The code I wrote is as follows:

public class optMain {

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO Auto-generated method stub long start, stop=0; long elpasedTime=0; start = System.nanoTime(); for (int i=0;i<100;i++) { System.out.println(" The number is "+i); } stop = System.nanoTime() - start; elpasedTime=stop-start; //calculate time in seconds for the println in for loop double printlntime=(double)elpasedTime / 1000000000.0; start = System.nanoTime(); for (int i=0;i<100;i++) { System.out.printf(" The number is %d ",i); } stop = System.nanoTime() - start; elpasedTime=stop-start; double printfTime=(double)elpasedTime / 1000000000.0; //calculate time in seconds for the printf in for loop System.out.println("It took "+printlntime+" seconds for code to run println "); System.out.printf("It took "+printfTime+" seconds for code to run printf"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago

Question

1. Diagnose and solve a transfer of training problem.

Answered: 1 week ago