Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/time.h>
- #define ASIZE 100000
- #define ITERS 5000
- int prdel[ASIZE];
- double get_time(void) {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;
- }
- int cmp_ints(const void * a, const void * b) {
- return *(int*)a<*(int*)b ? -1 : (*(int*)a==*(int *)b ? 0 : 1);
- }
- int main(int argc, char * argv[]) {
- int i,j;
- double start, finish;
- long sum = 0;
- srand(1337);
- for (i = 0; i < ASIZE; i++) {
- prdel[i] = rand();
- }
- // Fill array with random numbers
- for (i = 0; i < ASIZE; i++) {
- prdel[i] = rand();
- }
- start = get_time();
- // If WTF specified on the cmdline, sort the array
- if (argc == 2 && !strcmp(argv[1], "WTF")) {
- qsort(prdel, ASIZE, sizeof(int), cmp_ints);
- }
- // calculate sum of all "large" numbers in the array, repeat a couple of times.
- for(j = 0; j < ITERS; j++) {
- for(i = 0; i < ASIZE; i++) {
- if (prdel[i] >= RAND_MAX/2) {
- sum += prdel[i];
- }
- }
- }
- finish = get_time();
- // print result and time taken
- printf("Sum: %ld\nTime: %g\n", sum, finish - start);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement