Run the following pieces of code and see how long they take for N = 10, N = 300.
ID: 3890057 • Letter: R
Question
Run the following pieces of code and see how long they take for N = 10, N = 300. (Also try N = 1000). Pay attention to the following and BRIEFLY write your observations (e.g. for each value of N say: "it ran in less than a second", "several seconds, minutes", "I got tired of waiting after this much time", ...). Remember that you are in the College of Engineering. We do not want novels, we want information to the point. Therefore make the information stand-out: use a table, bullet points, fewer stands-out if I glance at it and I can easily see the content.) the runtime effect of replacing the 'res = res+1' with the 'print...' instruction for runtime_ increment and runtime_ print. how the runtime depends on the value of N for runtime_ increment how much faster (i.e. for smaller values of N) the runtime_ pow becomes too slow. // run for N = 10, N = 100, N = 1000 void runtime _increment (int N) { int i, k, t, res = 0: for (i = 1: iExplanation / Answer
program 1
i haave written the code in java to calculate the time taken to execute the program
public class demo1 {
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
runtime_increment(1000);
long endTime = System.currentTimeMillis();
System.out.println("Time taken is "+(endTime-startTime));
}
public static void runtime_increment(int N) {
int i, k, t, res =0;
for(i=1;i<=N;i++) {
for(k=1;k<=i;k++) {
for(t=1;t<=N;t++) {
res = res+1;
}
}
}
}
}
N time taken(ms)
10 0
100 3
1000 844
program 2
i have written the code in java and replaced the line printf("A") with system.out.println
public class IntToFloat {
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
runtime_increment(300);
long endTime = System.currentTimeMillis();
System.out.println("Time taken is "+(endTime-startTime));
}
public static void runtime_increment(int N) {
int i, k, t, res =0;
for(i=1;i<=N;i++) {
for(k=1;k<=i;k++) {
for(t=1;t<=N;t++) {
System.out.println("A");
}
}
}
}
}
N time taken(ms)
10 49
100 9609
300 249490
1000
program3
package snippet;
public class demo3 {
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
runtime_pow(30);
long endTime = System.currentTimeMillis();
System.out.println("Time taken is "+(endTime-startTime));
}
public static void runtime_pow(int N) {
int i, res =0;
for(i=1;i<=Math.pow(2.0,N);i++)
res = res+1;
}
}
N time taken(ms)
10 1
15 8
20 207
25 5727
30 188115
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.