С++. What is faster and why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

С++. What is faster and why?

void test1(unsigned char **matrix) { unsigned int start = clock(); int counter = 0; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(matrix[i][j] >= 128) { counter++; } } } unsigned int end = clock(); printf("%d\n", end-start); } void test2(unsigned char **matrix) { unsigned int start = clock(); int counter = 0; for(int j = 0; j < N; j++) { for(int i = 0; i < N; i++) { if(matrix[i][j] >= 128) { counter++; } } } unsigned int end = clock(); printf("%d\n", end-start); } void test3(unsigned char **matrix) { unsigned int start = clock(); int counter = 0; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { counter+=matrix[i][j]>>7; } } unsigned int end = clock(); printf("%d\n", end-start); } int main(int argc, char **argv) { unsigned char **matrix = (unsigned char **)malloc(N * sizeof(unsigned char *)); for(int i = 0; i < N; i++) { matrix[i] = (unsigned char *)malloc(N * sizeof(unsigned char *)); for(int j = 0; j < N; j++) { matrix[i][j] = rand() % 255; } } test1(matrix); test2(matrix); test3(matrix); return 0; }

17th Jun 2019, 7:35 PM
Ильдар Нафигов
Ильдар Нафигов - avatar
2 Answers
+ 2
Thank you for answer
17th Jun 2019, 8:58 PM
Ильдар Нафигов
Ильдар Нафигов - avatar
+ 1
please wrote comment to the code :)
18th Jun 2019, 4:19 AM
Ильдар Нафигов
Ильдар Нафигов - avatar