For the assignment, be sure to run unmodified code, but you are free to play around with the code to try out different parameters or different implementations. The functions don't necessarily do anything in particular, but does a lot of basic operations on 8 variables. Note that function1 and function2 do NOT produce the same result, but they do similar amounts of operations. Look at the differences between function1 and function2 What is the difference (apart from what the function does)? Do the two functions do the same amount of computation? If so, do you think that the two functions will run in the same amount of time? #include #include #include int function1(int a, int b, int c, int d, int e, int f, int g, int h, int n) { for (int i=0; i
For the assignment, be sure to run unmodified code, but you are free to
play around with the code to try out different parameters or different implementations.
The functions don't necessarily do anything in particular, but does a
lot of basic operations on 8 variables. Note that function1 and function2
do NOT produce the same result, but they do similar amounts of operations.
Look at the differences between function1 and function2
What is the difference (apart from what the function does)?
Do the two functions do the same amount of computation?
If so, do you think that the two functions will run in the same amount of
time?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int function1(int a, int b, int c, int d, int e, int f, int g, int h, int n) {
for (int i=0; i<n; i++) {
a = h + g;
b = a - h;
c = b + a;
d = c - b;
e = d + c;
f = e - d;
g = f + e;
h = g - f;
}
return h;
}
int function2(int a, int b, int c, int d, int e, int f, int g, int h, int n) {
for (int i=0; i<n; i++) {
h = g - f;
g = f + e;
f = e - d;
e = d + c;
d = c - b;
c = b + a;
b = a - h;
a = h + g;
}
return h;
}
// This is just the main which does all of the running and printing out
// You should be able to understand this code, but it is not required for the lab
int main(void) {
// Variables
int a,b,c,d,e,f,g,h;
int res;
clock_t start, end;
int numiters = 100000000;
// Benchmark function1
a = 1;
b = 1;
c = 1;
d = 1;
e = 1;
f = 1;
g = 1;
h = 1;
start = clock();
res = function1(a,b,c,d,e,f,g,h,numiters);
end = clock();
double seconds = (end - start)/((double)CLOCKS_PER_SEC);
printf("function1 (%d iterations):\n", numiters);
printf("\tThe result of the function is: %d\n", res);
printf("\ttime: %f seconds\n",seconds);
printf("\n");
// Benchmark function2
a = 1;
b = 1;
c = 1;
d = 1;
e = 1;
f = 1;
g = 1;
h = 1;
start = clock();
res = function2(a,b,c,d,e,f,g,h,numiters);
end = clock();
seconds = (end - start)/((double)CLOCKS_PER_SEC);
printf("function2 (%d iterations):\n", numiters);
printf("\tThe result of the function is: %d\n", res);
printf("\ttime: %f seconds\n",seconds);
printf("\n");
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps