Question

In: Computer Science

write three functions in C++: one that declares a large array statically, one that declares the...

write three functions in C++: one that declares a large array statically, one that declares the same array on the stack, and one that creates the same array on the heap. Call each of them a number of times (at least 100,000) and output the time required by each. Explain the results.

Solutions

Expert Solution

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

void static_allocation()

{

    static int array[400000]; //static array memory is alloted in program's data segment

}

void allocation_in_stack_memory()

{

    int array1[400000]; // non static array memmory allocation is done on stack, internally it calls alloca() to allocate memory which allocates from stack

}

void allocation_in_heap_memory()

{

    int *array2 = (int *)malloc(400000 * sizeof(int)); // malloc allocates memory from heap memory

}

int main()

{

    struct timespec time_1, time_2;

    int i;

    //for static allocation

    clock_gettime(CLOCK_REALTIME, &time_1);

    for (i = 0; i < 100000; i++)

        static_allocation();

    clock_gettime(CLOCK_REALTIME, &time_2);

    printf("To do 100000 times static allocation of array size 400000, it took %d nanoseconds\n", time_2.tv_nsec - time_1.tv_nsec);

    //for allocation on stack memory

    clock_gettime(CLOCK_REALTIME, &time_1);

    for (i = 0; i < 100000; i++)

        allocation_in_stack_memory();

    clock_gettime(CLOCK_REALTIME, &time_2);

    printf("To do 100000 times allocation of array size 400000 on the stack memory, it took %d nanoseconds\n", time_2.tv_nsec - time_1.tv_nsec);

    //for allocaion from heap memory

    clock_gettime(CLOCK_REALTIME, &time_1);

    for (i = 0; i < 100000; i++)

        allocation_in_heap_memory();

    clock_gettime(CLOCK_REALTIME, &time_2);

    printf("To do 100000 times allocation of array size 400000 from heap memory, it took %d nanoseconds\n", difftime(time_2.tv_sec, time_1.tv_sec));

    return 0;

}

SAMPLE OUTPUT:

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION


Related Solutions

Write a C++ program that declares three one-dimensional arrays named miles, gallons and mpg. Each array...
Write a C++ program that declares three one-dimensional arrays named miles, gallons and mpg. Each array should be capable of holding 10 elements. In the miles array, store the numbers 240.5, 300.0 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4 and 192.3. In the gallons array, store the numbers 10.3, 15,6, 8.7, 14, 16.3, 15.7, 14.9, 10.7 , 8.3 and 8.4. Each element of the mpg array should be calculated as the corresponding element of the miles array divided by the...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
Write a program in C++ that declares an array of 100 integers named scores[]. Prompt the...
Write a program in C++ that declares an array of 100 integers named scores[]. Prompt the user for how many scores they want to enter. Then read in the specified number of ints and store them in the array. Then prompt the user for a passing grade. Use a for loop to go trough the array and count how many scores are passing. Print the count of how many passing scores, and also print a double that is the percent...
*Java* Write an application that declares an array of three Listings whose contents are input by...
*Java* Write an application that declares an array of three Listings whose contents are input by the user (User inputs name and age for each listing). After the input, the listings should output in reverse order.
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT