Can someone look at my code and tell me why my sequential access was slower than my random access. I thought random was supposed to be slower most of the time. #include #include #include #define ARRAY_SIZE (1 << 24) void sequentialAccess(int* array) { for (int i = 0; i < ARRAY_SIZE; ++i) { array[i] += 1; // Accеssing еach еlеmеnt sеquеntially } } void randomAccess(int* array, int stride) { for (int i = 0; i < ARRAY_SIZE; i += stride) { array[i] += 1; // Accеssing еlеmеnts with a spеcifiеd stridе } } int main() { int* largeArray1 = (int*)malloc(ARRAY_SIZE * sizeof(int)); int* largeArray2 = (int*)malloc(ARRAY_SIZE * sizeof(int)); for (int i = 0; i < ARRAY_SIZE; ++i) { largeArray1[i] = i; largeArray2[i] = i; } clock_t startTimeSeq = clock(); sequentialAccess(largeArray1); clock_t endTimeSeq = clock(); clock_t startTimeRand = clock(); randomAccess(largeArray2, 2); // Change the stride as needed clock_t endTimeRand = clock(); double timeSeq = ((double)(endTimeSeq - startTimeSeq)) / CLOCKS_PER_SEC; double timeRand = ((double)(endTimeRand - startTimeRand)) / CLOCKS_PER_SEC; printf("Sequential Access Time: %f seconds\n", timeSeq); printf("Random Access Time: %f seconds\n", timeRand); free(largeArray1); free(largeArray2); return 0; } Sequential Access Time: 0.085000 seconds pc Random Access Time: 0.031000 seconds for my pc
Can someone look at my code and tell me why my sequential access was slower than my random access. I thought random was supposed to be slower most of the time. #include #include #include #define ARRAY_SIZE (1 << 24) void sequentialAccess(int* array) { for (int i = 0; i < ARRAY_SIZE; ++i) { array[i] += 1; // Accеssing еach еlеmеnt sеquеntially } } void randomAccess(int* array, int stride) { for (int i = 0; i < ARRAY_SIZE; i += stride) { array[i] += 1; // Accеssing еlеmеnts with a spеcifiеd stridе } } int main() { int* largeArray1 = (int*)malloc(ARRAY_SIZE * sizeof(int)); int* largeArray2 = (int*)malloc(ARRAY_SIZE * sizeof(int)); for (int i = 0; i < ARRAY_SIZE; ++i) { largeArray1[i] = i; largeArray2[i] = i; } clock_t startTimeSeq = clock(); sequentialAccess(largeArray1); clock_t endTimeSeq = clock(); clock_t startTimeRand = clock(); randomAccess(largeArray2, 2); // Change the stride as needed clock_t endTimeRand = clock(); double timeSeq = ((double)(endTimeSeq - startTimeSeq)) / CLOCKS_PER_SEC; double timeRand = ((double)(endTimeRand - startTimeRand)) / CLOCKS_PER_SEC; printf("Sequential Access Time: %f seconds\n", timeSeq); printf("Random Access Time: %f seconds\n", timeRand); free(largeArray1); free(largeArray2); return 0; } Sequential Access Time: 0.085000 seconds pc Random Access Time: 0.031000 seconds for my pc
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Can someone look at my code and tell me why my sequential access was slower than my random access. I thought random was supposed to be slower most of the time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE (1 << 24)
void sequentialAccess(int* array) {
for (int i = 0; i < ARRAY_SIZE; ++i) {
array[i] += 1; // Accеssing еach еlеmеnt sеquеntially
}
}
void randomAccess(int* array, int stride) {
for (int i = 0; i < ARRAY_SIZE; i += stride) {
array[i] += 1; // Accеssing еlеmеnts with a spеcifiеd stridе
}
}
int main() {
int* largeArray1 = (int*)malloc(ARRAY_SIZE * sizeof(int));
int* largeArray2 = (int*)malloc(ARRAY_SIZE * sizeof(int));
for (int i = 0; i < ARRAY_SIZE; ++i) {
largeArray1[i] = i;
largeArray2[i] = i;
}
clock_t startTimeSeq = clock();
sequentialAccess(largeArray1);
clock_t endTimeSeq = clock();
clock_t startTimeRand = clock();
randomAccess(largeArray2, 2); // Change the stride as needed
clock_t endTimeRand = clock();
double timeSeq = ((double)(endTimeSeq - startTimeSeq)) / CLOCKS_PER_SEC;
double timeRand = ((double)(endTimeRand - startTimeRand)) / CLOCKS_PER_SEC;
printf("Sequential Access Time: %f seconds\n", timeSeq);
printf("Random Access Time: %f seconds\n", timeRand);
free(largeArray1);
free(largeArray2);
return 0;
}
Sequential Access Time: 0.085000 seconds pc
Random Access Time: 0.031000 seconds for my pc
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education