C++ 自下而上合并排序的奇怪执行时间

C++ 自下而上合并排序的奇怪执行时间,c++,mergesort,C++,Mergesort,我有一些正在进行的项目,我需要使用自下而上的合并排序。 在做这件事的时候,我注意到了一些奇怪的事情 下面是代码: #include <iostream> #include <fstream> #include <chrono> #include <algorithm> using namespace std; typedef std::chrono::high_resolution_clock Clock; void populateByRan

我有一些正在进行的项目,我需要使用自下而上的合并排序。 在做这件事的时候,我注意到了一些奇怪的事情

下面是代码:

#include <iostream>
#include <fstream>
#include <chrono>
#include <algorithm>

using namespace std;
typedef std::chrono::high_resolution_clock Clock;

void populateByRandom(int arr[], int n);
void resetArray(int arr[], int n);
void merge1(int arr[], int l, int m, int r);
void mergeSort1(int arr[], int n);
int min(int x, int y);

int main()
{
    srand(time(NULL));
    ofstream myfile ("AdvancedSortsTest.txt");
    int n;
    int exponent[3]={13, 17, 21};
    int *arr;
    for(int i=0; i<1; i++)
    {
        n=(2 << exponent[i]);
        arr=new int[n];
        myfile << "For array of size\t" << n << ":" << endl << endl;
        for(int j=0; j<1000; j++)
        {
//          resetArray(arr, n);
            populateByRandom(arr, n);
            auto start=Clock::now();
            mergeSort1(arr, n);
            auto end=Clock::now();
            myfile << std::chrono::duration_cast<std::chrono::microseconds>(end-start).count() << endl;
        }
        myfile << endl << endl;
        delete [] arr;
    }
    myfile << endl << "*(time in microseconds)";
    myfile.close();
    return 0;
}

/* Iterative mergeSort1 function to sort arr[0...n-1] */
void mergeSort1(int *arr, int n)
{
    int curr_size;  // For current size of subarrays to be merged
    // curr_size varies from 1 to n/2
    int left_start; // For picking starting index of left subarray
    // to be merged

    // Merge subarrays in bottom up manner.  First merge subarrays of
    // size 1 to create sorted subarrays of size 2, then merge subarrays
    // of size 2 to create sorted subarrays of size 4, and so on.
    for (curr_size=1; curr_size<=n-1; curr_size = 2*curr_size)
    {
        // Pick starting point of different subarrays of current size
        for (left_start=0; left_start<n-1; left_start += 2*curr_size)
        {
            // Find ending point of left subarray. mid+1 is starting
            // point of right
            int mid = left_start + curr_size - 1;

            int right_end = min(left_start + 2*curr_size - 1, n-1);

            // Merge Subarrays arr[left_start...mid] & arr[mid+1...right_end]
            merge1(arr, left_start, mid, right_end);
        }
    }
}

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
void merge1(int *arr, int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;
    /* create temp arrays */
    int *L=new int[n1];
    int *R=new int[n2];
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
    delete [] L;
    delete [] R;
}

// Utility function to find minimum of two integers
int min(int x, int y)
{
    return (x<y)? x :y;
}


void populateByRandom(int arr[], int n)
{
    for (unsigned int i = 0; i < n; i += 1)
        arr[i]=rand()%1000000;
}
void resetArray(int arr[], int n)
{
    for (unsigned int i = 0; i < n; i += 1)
        arr[i]=0;
}
*(以微秒为单位的时间)

我的问题是——为什么前几类要比其他几类花更多的时间?我已经运行了很多次这个程序,有时是前20-30次,有时更少,但总是至少第一次需要更多的时间。 我相信这和函数merge1中的动态内存分配有关,但我真的不知道具体是什么


编辑:当我更改main中的外部“for”函数,使其迭代两次,数组大小为2^14和2^18时,合并排序在数组2^14中的第一次迭代仍然需要更长的时间,但2^18数组的情况并非如此(此处的执行时间是统一的)

uhmm。。。我不是C++,但是你必须重复很多次吗?这让这个问题读起来很奇怪。。。亲爱的上帝,密码的数量。。。这是问题的全部要求吗?或者你只是想找人来调试你的整个程序,因为你不能在stackoverflow上这么做……我确实需要反复这么多次才能确切地说明问题所在。以显示执行时间减少,但下降到某个点。是的,这个问题需要所有的代码,因为我不知道问题在哪里。是吗?没有,但我很擅长发现那些可能无法得到答案的问题(很多代码)。我不做C++@damaxcontent,正如我在问题中提到的,我的直觉告诉我,问题在于函数merge1()中的动态内存分配,我只能放这段代码,但我担心我只能得到这样的答案:“你可能以错误的方式测量时间,显示整个代码”,等等。@sergej实际上就是这样——动态频率缩放。非常感谢,现在我确定我今天的测量结果都不正确:|嗯。。。我不是C++,但是你必须重复很多次吗?这让这个问题读起来很奇怪。。。亲爱的上帝,密码的数量。。。这是问题的全部要求吗?或者你只是想找人来调试你的整个程序,因为你不能在stackoverflow上这么做……我确实需要反复这么多次才能确切地说明问题所在。以显示执行时间减少,但下降到某个点。是的,这个问题需要所有的代码,因为我不知道问题在哪里。是吗?没有,但我很擅长发现那些可能无法得到答案的问题(很多代码)。我不做C++@damaxcontent,正如我在问题中提到的,我的直觉告诉我,问题在于函数merge1()中的动态内存分配,我只能放这段代码,但我担心我只能得到这样的答案:“你可能以错误的方式测量时间,显示整个代码”,等等。@sergej实际上就是这样——动态频率缩放。非常感谢,现在我确定我今天的所有测量值都不正确:|
14935
9303
8525
7568
6316
7366
6263
5902
5752
5589
5364
5485
5121
5108
5214
4870
4759
4760
4625
4454
4479
4545
4272
4254
4211
4151
4062
3957
4062
3875
3819
3824
3803
3693
3729
3621
3649
3594
3558
3458
3463
3635
3085
3074
3109
3101
3425
3140
3092
3079
3098
3350
3075
3069
3077
3066
3259
3168
3090
3129
3097
3341
3161
3243
3132
3183
3394
3181
3152
3183
3172
3423
3179
3172
3132
3284
3151
3167
3191
3117
3205
3209
3132
3132
3120
3296
3162
3147
3110
3210
3242
3142
3067
3193
3128
3155
3103
3234
3404
3191