Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Instruction order in C/C++
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> int compute1 (char a[], int start, int end) { int sum = 0; int min = a[start]; int max = a[start]; for (int i = start; i < end; i++) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; int cube = a[i] * a[i] * a[i]; sum += cube; } return sum; } int compute2 (char a[], int start, int end) { int sum = 0; int min = a[start]; int max = a[start]; for (int i = start; i < end; i++) { int cube = a[i] * a[i] * a[i]; sum += cube; if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } return sum; } void initArray (char * array, int size) { int intsInArray = size / sizeof(int); int trailing = size % sizeof(int); int * arr = (int *) array; int i; for (i = 0; i < intsInArray; i++) { arr[i] = rand(); } int random = rand(); int j; i *= 4; for (j = 0; j < trailing; j++) { char byte = random & 0xFF; random >>= 8; array[i + j] = byte; } } int main (int argc, char *argv[]) { struct timeval t0, t1; int result; int size = 50000000; char * array = (char *) malloc(size); initArray(array, size); double runtime; for (int i = 0; i < 2; i++) { /* Min/max */ (void) printf("Min/max first: "); gettimeofday(&t0, NULL); // start time result = compute1(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n", runtime); /* Product-sum */ (void) printf("Product-sum first: "); gettimeofday(&t0, NULL); // start time result = compute2(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n\n", runtime); } printf("================\n\n"); for (int i = 0; i < 2; i++) { /* Product-sum */ (void) printf("Product-sum first: "); gettimeofday(&t0, NULL); // start time result = compute2(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n", runtime); /* Min/max */ (void) printf("Min/max first: "); gettimeofday(&t0, NULL); // start time result = compute1(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n\n", runtime); } printf("================\n\n"); for (int i = 0; i < 2; i++) { /* Min/max */ (void) printf("Min/max first: "); gettimeofday(&t0, NULL); // start time result = compute1(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n", runtime); /* Product-sum */ (void) printf("Product-sum first: "); gettimeofday(&t0, NULL); // start time result = compute2(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n", runtime); /* Product-sum */ (void) printf("Product-sum first: "); gettimeofday(&t0, NULL); // start time result = compute2(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n", runtime); /* Min/max */ (void) printf("Min/max first: "); gettimeofday(&t0, NULL); // start time result = compute1(array, 0, size); gettimeofday(&t1, NULL); // end time runtime = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0; (void) printf("Completed in %f sec\n\n", runtime); } free(array); return 0; }
run
|
edit
|
history
|
help
0
pattern 12
B_141125_Burbuja
Command Expressions in Gnu C: What Does Return Return?
Atoi without minus
Bitwise Operations
Anutter juan
stringify
Float data type.c
3468
ptr_tp_ptr