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\n",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"); } }
Please Send me answer within 10 min!! I will rate you good for sure!!
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\n",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
Solved in 4 steps with 7 images