Click here to Skip to main content
15,889,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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();
}
Posted
Updated 7-Sep-17 6:01am
v2
Comments
OriginalGriff 7-Sep-17 5:13am    
No question is asked either - and until you do, there isn't a lot we can do to help you...
Javier Luis Lopez 7-Sep-17 12:03pm    
Sorry I made a mistake due my bad english I should say just the opposite: no question is asked

1 solution

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.
 
Share this answer
 
Comments
Javier Luis Lopez 7-Sep-17 5:37am    
You are true. fill not only can fills with zeroes a float array but also can be used to fill array with anything. It is a great advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900