Esempio n. 1
0
int main(void) {
    int i, sorted;
    int *arrayS = malloc(sizeof(int) * MAXN);
    int *arrayP = malloc(sizeof(int) * MAXN);

    unsigned int seed = time(NULL);

    long start_sequential, end_sequential, start_parallel, end_parallel;

    double calc, sequential, parallel;

    for (i = 0; i < MAXN; i++) {
        arrayS[i] = rand_r(&seed);
        arrayP[i] = rand_r(&seed);
    }

    arrayIsSorted(arrayS);

    struct timespec start_sequential_time;
    clock_gettime(CLOCK_MONOTONIC, &start_sequential_time);
    start_sequential = start_sequential_time.tv_sec * NANOSECS_PER_SEC + 
        start_sequential_time.tv_nsec;

    sequentialCountSort(arrayS);

    struct timespec end_sequential_time;
    clock_gettime(CLOCK_MONOTONIC, &end_sequential_time);
    end_sequential = end_sequential_time.tv_sec * NANOSECS_PER_SEC + 
        end_sequential_time.tv_nsec;

    sequential = (double)(end_sequential - start_sequential) / NANOSECS_PER_SEC;

    arrayIsSorted(arrayS);
    printf("Sequential time = %.4f sec.\n", sequential);

    arrayIsSorted(arrayP);
    struct timespec start_parallel_time;
    clock_gettime(CLOCK_MONOTONIC, &start_parallel_time);
    start_parallel = start_parallel_time.tv_sec * NANOSECS_PER_SEC + 
        start_parallel_time.tv_nsec;

    parallelCountSort(arrayP);

    struct timespec end_parallel_time;
    clock_gettime(CLOCK_MONOTONIC, &end_parallel_time);
    end_parallel = end_parallel_time.tv_sec * NANOSECS_PER_SEC + 
        end_parallel_time.tv_nsec;

    parallel = (double)(end_parallel - start_parallel) / NANOSECS_PER_SEC;

    arrayIsSorted(arrayP);
    printf("Parallel time = %.4f sec.\n", parallel);

    return 0;
}
Esempio n. 2
0
double sortArrayWithBenchmark(uint32_t arr[], const unsigned int count, SortBenchmark benchmark)
{
    uint32_t arrCopy[count];
    memcpy(arrCopy, arr, sizeof(uint32_t) * count);

    printf("Running %s sort...\n", benchmark.algorithm);

    double startTime = clock();

    benchmark.func(arrCopy, count);

    double endTime = clock();

    assert(arrayIsSorted(arrCopy, count));

    double totalTime = (endTime - startTime) / CLOCKS_PER_SEC;

    printf("%s sort finished in %lf sec\n\n", benchmark.algorithm, totalTime);

    return totalTime;
}