Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views2 pages

Practical 9

Uploaded by

Muskan Jangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Practical 9

Uploaded by

Muskan Jangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

9. Evaluate the impact of caching and paging strategies on system performance.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define ARRAY_SIZE 1000000 // Size of the array (large to stress paging)

#define CACHE_SIZE 1024 // Simulate a small cache size (to affect cache performance)

// Function to measure the time for sequential access (cache-friendly)

void measure_cache_performance() {

int *array = (int*)malloc(ARRAY_SIZE * sizeof(int));

for (int i = 0; i < ARRAY_SIZE; i++) {

array[i] = i; }

clock_t start_time = clock();

int sum = 0;

for (int i = 0; i < ARRAY_SIZE; i++) {

sum += array[i]; }

clock_t end_time = clock();

double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;

printf("Cache-friendly access time: %.6f seconds\n", time_spent);

free(array);

// Function to measure the time for random access (paging-friendly)

void measure_paging_performance() {

int *array = (int*)malloc(ARRAY_SIZE * sizeof(int));

for (int i = 0; i < ARRAY_SIZE; i++) {

array[i] = i; }

clock_t start_time = clock();

int sum = 0;

for (int i = 0; i < ARRAY_SIZE; i++) {

int index = rand() % ARRAY_SIZE; // Random index, simulating random access

sum += array[index]; }
clock_t end_time = clock();

double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;

printf("Random access time (paging impact): %.6f seconds\n", time_spent);

free(array);

int main() {

printf("Measuring Cache Performance...\n");

measure_cache_performance();

// Measure paging performance (random access)

printf("Measuring Paging Performance...\n");

measure_paging_performance();

return 0;

Output:-

Measuring Cache Performance...

Cache-friendly access time: 0.002642 seconds

Measuring Paging Performance...

Random access time (paging impact): 0.027480 seconds

You might also like