本文介绍了填充阵列快5倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码填充数组比在发布模式下使用memset快5倍,不幸的是在调试模式下比较慢。 没问题。 我尝试过:
#include < iostream > #include < time.h > // clock( ) #pragma warning(disable:4996)//禁用不推荐使用 使用 命名空间标准; typedef unsigned char uchar ; time_t start,stop; // 计时功能。将此命令添加到要测量的函数的开头:start = clock(); void timer( char * title, int size = 0 ) { stop = clock(); cout<< title<< time =<<( double )(stop-start)/( double )CLOCKS_PER_SEC<< secs; if (size) cout<< =<< 1e- 6 * size /(( double )(stop-start)/( double )CLOCKS_PER_SEC)<< Mops / seg<< endl; else cout<< endl; start = clock(); // 必须在要测量的函数的开头做得更好 } #define SIZE 100000000 // 100Meg void main() { cout<< 警告:在RELEASE模式下运行该程序。在DEBUG模式下,时间不正确<< endl; float * data = new float [SIZE]; start = clock(); memset(数据, 0 ,SIZE * sizeof (浮)); timer( \ n memset(data,0,SIZE * sizeof(float)),SIZE); // 这在发布模式下可能快5倍!: std: :fill(data,data + SIZE, 3 .14f); timer( \ n fill(data,data + SIZE,3.14f) ,尺寸); 删除数据; cout<< === END ===<< endl ;的getchar(); }解决方案
memset 和 std :: fill ,是不同的函数,它们不应该完全相同。 我不会尝试在调试版本中测量函数性能。
The following code fills arrays 5 times faster than using memset in release mode, unfortunately is slower in debug mode. No question is asked. What I have tried:
#include <iostream> #include <time.h> //clock() #pragma warning(disable:4996) //disable deprecateds using namespace std; typedef unsigned char uchar; time_t start,stop; //Timing function. Add this command to the beginning of the function to be measured: start=clock(); void timer(char *title,int size=0) { stop=clock(); cout<<title<< " time ="<<(double) (stop-start)/(double) CLOCKS_PER_SEC<< " secs"; if (size) cout << " = " << 1e-6*size/( (double)(stop-start)/(double)CLOCKS_PER_SEC ) << " Mops/seg" <<endl; else cout<<endl; start=clock();//it must be done better in the beginning of the function to be measured } #define SIZE 100000000 //100Meg void main() { cout<<"WARNING: run this program in RELEASE mode. Timing is non correct in DEBUG mode"<<endl; float *data=new float[SIZE]; start=clock(); memset(data,0,SIZE*sizeof(float)); timer("\n memset(data,0,SIZE*sizeof(float)) ",SIZE); //This may be 5x faster in release mode!: std::fill(data, data+SIZE, 3.14f); timer("\n fill(data, data+SIZE, 3.14f) ",SIZE); delete data; cout<<"===END==="<<endl;getchar(); } 解决方案 memset and std::fill, are different functions, they are not supposed to perform exactly the same. I wouldn't ever attempt to measure function performance in a debug build.更多推荐
填充阵列快5倍
发布评论