c ++排序算法持续时间

时间:2012-11-16 00:44:17

标签: c++ algorithm sorting time duration

我一直致力于计算这些排序算法的持续时间。我将所有排序方法循环2000次,然后将总持续时间分成2000,以获得适当的持续时间值。问题是;它没有显示排序方法的特定代码部分所用的确切时间值。我的意思是duration变量通过程序流显示增加的值。例如,对于N = 10000insertionSort()给出0.000635,mergeSort()给出0.00836而heapSort()给出0.018485,当我改变这些的顺序时,duration仍然通过程序,无论算法类型如何。我尝试为每个进程提供不同的持续时间值,但这不起作用。有人可以帮助我理解这个问题,还是有其他时间测量风格?

对不起,如果这是一个愚蠢的问题和我糟糕的语法。

int main(){

    srand(time(NULL));

    int N, duration;

    cout << endl << "N : ";
    cin >> N; // N is array sze.
    cout << endl;

    // a4 would be the buffer array (for calculating proper duration).
    int *a1 = new int[N];
    int *a2 = new int[N];
    int *a3 = new int[N];
    int *a4 = new int[N];

    cout << endl << "Unsorted array : " << endl;

    for (int i = 0; i < N; i++){

        a4[i] = rand() % 100;
        cout << a4[i] << " ";
    }

/*------------------------------------------------------------------------------*/

    cout << endl << endl <<"Sorting with Insertion Sort, please wait..." << endl;

    for(int i = 0; i < 2000; i++){

        a1 = a4;

        duration = clock();
        insertionSort(a1, N - 1);
        duration += clock() - duration;
    }

    cout << endl << "Insertion sort : " << endl;

    print(a1, N);

    cout << endl << endl << "Approximate duration for Insertion Sort : ";
    cout << (double) (duration / 2000) / CLOCKS_PER_SEC;
    cout << " s." << endl;

/*------------------------------------------------------------------------------*/

    cout << endl << endl << "Sorting with Merge Sort, please wait..." << endl;

    for(int i = 0; i < 2000; i++){

        a2 = a4;

        duration = clock();
        mergeSort(a2, 0, N - 1);
        duration += clock() - duration;
    }

    cout << endl << "Merge sort : " << endl;

    print(a2, N);

    cout << endl << endl << "Approximate duration for Merge Sort : ";
    cout << (double) (duration / 2000) / CLOCKS_PER_SEC;
    cout << " s."<< endl << endl;

/*------------------------------------------------------------------------------*/

    cout << endl << endl << "Sorting with Heap Sort, please wait..." << endl;

    for(int i = 0; i < 2000; i++){

        a3 = a4;
        duration = clock();
        heapSort(a3, N);
        duration += clock() - duration;
    }

    cout << endl << "Heap sort : " << endl;

    print(a3, N);

    cout << endl << endl << "Approximate duration for Heap Sort : ";
    cout << (double) (duration / 2000) / CLOCKS_PER_SEC;
    cout << " s."<< endl << endl;

    return 0;
}

2 个答案:

答案 0 :(得分:7)

程序中的错误是您在整个循环中重置持续时间。处理时间的更简洁方法是将duration变量修改放在for循环之外。例如:

duration = clock();
for(int i = 0; i < 2000; i++){
    a2 = a4;
    mergeSort(a2, 0, N - 1);
}
duration = clock() - duration
编辑:忘了删除循环内的部分。现在修好了。

答案 1 :(得分:2)

第一,您似乎不会在不同排序的运行之间重置duration。这意味着各个迭代持续时间的总和将在每个排序阶段向下传播(如果下一个点也不是问题)。

接下来,您需要设置一个单独的变量,将其称为durationSum并在迭代后的摘要阶段使用duration。目前,你在每次迭代中都要把你的总和吹走。

例如:

clock_t durationSum = 0;
clock_t duration = 0;

for(int i = 0; i < 2000; i++){

    a1 = a4;

    duration = clock();
    insertionSort(a1, N - 1);
    durationSum += clock() - duration;
}

接下来,在分摊duration时,您会出现类型错误。你有:

cout << (double) (duration / 2000) / CLOCKS_PER_SEC;

使用最少的编辑,这可以更精确地工作(但应该使用durationSum):

cout << (double) (duration / 2000.0) / CLOCKS_PER_SEC;

在此之前,您说“使用整数除法将duration除以2000,然后将其提升为double并除以CLOCKS_PER_SEC(这次使用浮点除法,因为一个操作数是double和一个积分。使用2000.0强制duration在2000年之前被提升为浮点除法的双精度。

最后,与单个排序迭代相比,最好将循环开销视为可忽略不计,并且每2000次排序迭代只对clock()进行两次调用。

例如:

clock_t insert_sort_start = clock();

for(int i = 0; i < 2000; i++){
    a1 = a4;
    insertionSort(a1, N - 1);
}

double duration_sec = (clock() - insert_sort_start) / 2000.0 / CLOCKS_PER_SEC;

最后,请注意您使用duration作为int,而实际上它是clock_t,如果您使用的是64位系统,则很可能这是由clock()返回的64位数字,并且“缩小”(向下转换)为32位整数int。使用clock_t