Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a single program that call three functions in C. Have the three functions each create/initialize an array but have one that declares and initialize
Write a single program that call three functions in C. Have the three functions each create/initialize an array but have one that declares and initialize a large array statically (from data segment), one that declares and initialize the same size array on the stack, and one that creates and initialize the same size array from the heap. Call each of the subprograms a large number of times (at least 1,000) and output the average time required by each. Explain your results. For example, what are the trade-offs, in time and space, when the allocation of an arrays occurs in the runtime stack rather than the heap or data/static segment? By large size we mean > 10000000 Declaring and initialize the arrays via for loop in each function for int i=0; i< SIZE; i++) array[i]=i; For determining the time each takes could do something like the following:
#include
#include
#include
#include
#include
struct rusage ruse;
#define CPU_TIME (getrusage(RUSAGE_SELF,&ruse), ruse.ru_utime.tv_sec + \
ruse.ru_stime.tv_sec + 1e-6 * \
(ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec))
int main(void) {
time_t start, end;
double first, second;
// Save user and CPU start time
time(&start);
first = CPU_TIME;
// Perform operations
/* . . . */
// Save end time
time(&end);
second = CPU_TIME;
printf("cpu : %.2f secs ", second - first);
printf("user : %d secs ", (int)(end - start));
}
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