我正在对排序速度进行一些测量,并测量了对100万个浮点值进行排序所需的时间,所有这些时间都在[0,1 ]通过使用标准std :: sort在<algorithm>.
中进行排序在我的硬件上,带有6 GB Ram的intel core i5,下面的代码告诉我它大约需要1164.188毫秒。但是,我怀疑这是否正确,并想问这个测量是否正确。请看下面的代码,知道我是如何得到1164.188ms通过排序在算法库中排序100万个浮点数的测量时间
#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 found\n");
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 %f\n",(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();
}
你为什么认为这是错的? – templatetypedef 2011-12-25 18:51:22
sort()是什么实现?它不是任何标准库 - 可能是Windows特定的?它如何知道如何比较元素? – wallyk 2011-12-25 18:54:00
@wallyk现在Introsort非常普遍。或者至少是一种深度感知的方法。并且比较是模板化的。这里<。 – 2011-12-25 18:58:04