填充阵列快5倍

编程入门 行业动态 更新时间:2024-10-28 06:23:19
本文介绍了填充阵列快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倍

本文发布于:2023-10-18 11:42:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!