我正在對排序速度進行一些測量,並測量了對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