Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started