测量在算法库中对100万个0到1之间的浮点数进行排序的时间



我正在对排序速度进行一些测量,并正在测量对100万个浮点值进行排序所需的时间,所有值都在[0,1]之间,使用<algorithm>.中的标准std::sort在我的硬件上,英特尔core i5具有6 GB Ram,下面的代码显示我大约需要1164.188毫秒。然而,我怀疑这个的正确性,想问一下这个测量是否正确。请参阅下面的代码,了解我如何获得1164.188ms

#include<algorithm>
#include<iostream>
#include<windows.h>
using namespace std;
void main(){
    const int N = 1000000;
    FILE *f;
    f = fopen("invertedList.txt","r");
    if( f == NULL){
            printf("File not foundn");
            system("pause");
            exit(1);
    }
    int count = 0 ;
      //read input from a file
    float* a = (float*)malloc(N * sizeof(float));
    for( int i =0 ; i < N ; i++){
        fscanf(f, "%f,", &a[count]);
        count++;
    }
    fclose(f);
      //start the clock
    __int64 ctr1 = 0 , ctr2 = 0 , freq = 0 ;
    QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
    QueryPerformanceCounter((LARGE_INTEGER *) &ctr1);
    sort(a,a+N);
    QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);//stop clock
    double ans = ((ctr2 - ctr1) * 1.0 / freq);
    printf("The time elapsed in milliseconds is %fn",(ans*1000));
    FILE *tow;
    tow = fopen("writesort.txt","w");
    for(int i =0 ; i< N;i++){
        fprintf(tow,"%f,",a[i]);
    }
    free(a);
    fclose(tow);
    getchar();

}

我会对fscanf(f, "%f,", &a[count]);的返回值添加一个检查,以确保它读取并转换了一个值。

时序逻辑似乎合理。注意,它测量的是经过的时间。至于精确测量算法,只有在机器负载较轻或更少的情况下才能实现。多次运行可以给出时间有效性的指示:如果接近,则它们可能是准确的。如果有很大的变化(> 25%),则其他系统操作正在破坏计算。

最新更新