用vector实现对10W个随机数进行插入排序为什么这么慢?
在visual studio 2008下编译速度巨慢,要300多秒
而用intel c++ compiler编译就快了很多倍,只需要10秒左右
另外改成用数组实现也要快很多,不到20秒
请高手指点一下是什么原因?按道理说用数组和用vector,时间差距不应 该这么大啊
一开始以为是vector要做越界检查的原因,在程序开头加了#define _SECURE_SCL 0还是很慢
下面是源代码:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
int main()
{
int vectorSize;
cout<<"Please enter the size of vector: ";
cin>>vectorSize;
cout<<endl;
vector<float> v1; //the vectors to store the numbers to be sorted
srand(time(NULL)); // to generate differnt numbers for sort each time
//Initialize the vector v
for(int i=0;i<vectorSize;i++)
{
float rNum; //random number
rNum=(float)rand()/RAND_MAX; //generate random numbers
v1.push_back(rNum); //insert rNum into the vector v1
}
clock_t start,end; //starting time and ending time
start=clock();
insertionSort(v1);
end=clock();
cout<<float) (end-start)/ CLOCKS_PER_SEC <<" secs"<<endl;
return 0;
}
void insertionSort(vector<float>& v)
{
float temp;
for(int i=1;i< v.size();i++)
{
//v[0...i-1] is a sorted subarray
//find a correct postion for v[i] and insert v[i] to it
int j=i;
temp=v[i];
while(j>0&&temp<v[j-1])
{ //move the numbers that are greater than v[i]
v[j]=v[j-1];
j--;
}
v[j]=temp; //find the right postion and assign v[i] to it
}
}
而用intel c++ compiler编译就快了很多倍,只需要10秒左右
另外改成用数组实现也要快很多,不到20秒
请高手指点一下是什么原因?按道理说用数组和用vector,时间差距不应 该这么大啊
一开始以为是vector要做越界检查的原因,在程序开头加了#define _SECURE_SCL 0还是很慢
下面是源代码:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
int main()
{
int vectorSize;
cout<<"Please enter the size of vector: ";
cin>>vectorSize;
cout<<endl;
vector<float> v1; //the vectors to store the numbers to be sorted
srand(time(NULL)); // to generate differnt numbers for sort each time
//Initialize the vector v
for(int i=0;i<vectorSize;i++)
{
float rNum; //random number
rNum=(float)rand()/RAND_MAX; //generate random numbers
v1.push_back(rNum); //insert rNum into the vector v1
}
clock_t start,end; //starting time and ending time
start=clock();
insertionSort(v1);
end=clock();
cout<<float) (end-start)/ CLOCKS_PER_SEC <<" secs"<<endl;
return 0;
}
void insertionSort(vector<float>& v)
{
float temp;
for(int i=1;i< v.size();i++)
{
//v[0...i-1] is a sorted subarray
//find a correct postion for v[i] and insert v[i] to it
int j=i;
temp=v[i];
while(j>0&&temp<v[j-1])
{ //move the numbers that are greater than v[i]
v[j]=v[j-1];
j--;
}
v[j]=temp; //find the right postion and assign v[i] to it
}
}
...全文
相关推荐
Java实现生成n个不重复的随机数
主要介绍了Java实现生成n个不重复的随机数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
排序算法复杂度验证:从实验设计到性能拟合的完整指南
算法复杂度是评估程序性能的理论基石,时间复杂度分析帮助工程师在数据规模变化时预判运行时间。实际开发中,插入排序、归并排序与快速排序的复杂度差异,需要通过可控实验进行验证。通过分档输入规模、固定随机种子、精密计时与多次取中位数,可以消除系统扰动,获得可信的基准数据。将测量结果与理论模型拟合,辅以双对数坐标图斜率判读,可直观区分O(n^2)与O(n log n)量级。该流程不仅适用于课程实验,也能迁移到CI性能回归,成为日常工程中评估算法优化的通用方法。本文以排序算法为例,展示从数据生成到复杂度结论的完整实验设
C++ Vector 动态数组的实现
主要介绍了C++ Vector 动态数组的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
插入排序的C++实现
用C++实现的插入排序算法,其中并没有使用数组,而是使用了vector容器和迭代器。
C++ vector操作实现
主要介绍了C++ vector操作实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


