简体   繁体   English

c ++排序算法持续时间

[英]c++ sorting algorithms duration

I have been working on calculating duration of these sorting algorithms take. 我一直在努力计算这些排序算法的持续时间。 I looped all sorting methods 2000 times then divide total duration into 2000 to get a proper value for duration. 我将所有排序方法循环2000次,然后将总持续时间分成2000,以获得适当的持续时间值。 The problem is; 问题是; it does not show the exact value of time that the particular code parts of sorting methods take. 它没有显示排序方法的特定代码部分所用的确切时间值。 I mean the duration variable shows increasing values through the program flows. 我的意思是duration变量通过程序流显示增加的值。 For example, for N = 10000 , insertionSort() gives 0.000635, mergeSort() gives 0.00836 and heapSort() gives 0.018485 and when I change order of these, duration still goes up through program, regardless of algorithm type. 例如,对于N = 10000 ,insertSort insertionSort()给出0.000635, mergeSort()给出0.00836, heapSort()给出0.018485,当我改变这些的顺序时,无论算法类型如何, duration仍然会通过程序上升。 I tried giving different duration values for each process but that didn't work. 我尝试为每个进程提供不同的持续时间值,但这不起作用。 Can someone help me to understand this problem or are there any other time measuring styles? 有人可以帮助我理解这个问题,还是有其他时间测量风格?

Sorry if this is a dumb problem and for my bad grammar. 对不起,如果这是一个愚蠢的问题和我的坏语法。

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;
}

The error in your program is that you reset duration throughout the loop. 程序中的错误是您在整个循环中重置持续时间。 A cleaner way to handle the time would be to put the duration variable modification outside the for loop. 处理时间的更简洁方法是将duration变量修改放在for循环之外。 For example: 例如:

duration = clock();
for(int i = 0; i < 2000; i++){
    a2 = a4;
    mergeSort(a2, 0, N - 1);
}
duration = clock() - duration

EDIT: forgot to remove the part inside the loop. 编辑:忘了删除循环内的部分。 Fixed now. 现在修复了。

Number one, you don't seem to reset duration between runs of the different sorts. 第一,你似乎没有重置不同种类的运行之间的duration This means the sum of individual iteration durations would be propagating down through each sorting phase (if the next point weren't also a problem). 这意味着各个迭代持续时间的总和将在每个排序阶段向下传播(如果下一个点也不是问题)。

Next, you need to setup a separate variable, call it durationSum and use that as you are currently using duration in the summary phase after iterating. 接下来,您需要设置一个单独的变量,将其称为durationSum并使用它,因为您在迭代后在摘要阶段中使用duration Currently, you're blowing away your sum on every iteration. 目前,你每次迭代都要浪费你的金额。

For example: 例如:

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;
}

Next, you're making a type error when amortizing duration . 接下来,在分摊duration时,您会出现类型错误。 You have: 你有:

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

With minimal edits, this would work more precisely (but should use durationSum ): 使用最少的编辑,这将更精确地工作(但应使用durationSum ):

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

Before, you were saying "use integer division to divide duration by 2000, THEN promote it to a double and divide by CLOCKS_PER_SEC (this time with floating-point division because one of the operands is a double and one integral). Using 2000.0 forces duration to be promoted to a double for a floating-point division by 2000. 以前,你说“使用整数除法将duration除以2000,然后将它提升为一个double并除以CLOCKS_PER_SEC (这次是浮点除法,因为其中一个操作数是一个double 2000.0和一个积分)。使用2000.0强制duration到2000年被提升为浮点分区的双倍。

Finally, it would be better to consider the loop overhead negligible compared to a single sort iteration and do only two calls to clock() per 2000 sort-iterations. 最后,与单个排序迭代相比,最好将循环开销视为可忽略不计,并且每2000次排序迭代仅对clock()进行两次调用。

For example: 例如:

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;

Finally, note that you are using duration as an int whereas in reality it is a clock_t , and if you are on a 64-bit system, it is very likely that this is a 64-bit number being returned by clock() and "narrowed" (downcast) into a 32-bit integer int . 最后,请注意您将duration用作int而实际上它是一个clock_t ,如果您使用的是64位系统,则很可能这是由clock()返回的64位数字,并且缩小“(向下转换)为32位整数int Use clock_t . 使用clock_t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM