CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2008
    Posts
    23

    Memory clear for time testing in c++

    I need to measure computational time of my algorithm with different constants values.
    To get accurate results, I think I need to set memory in the same condition (e.g clear memory) before testing each time. My algorithm uses std::vector<double>. Are there any instruction in c++?

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Memory clear for time testing in c++

    What do you mean by "clear memory"? Free the memory reserved by a vector? One way that's often accomplished is what's called the swap trick:
    Code:
    std::vector<double> lots_of_data(5000);
    
    lots_of_data.swap(std::vector<double>());
    // capacity of lots_of_data is now 0
    If by clear memory you mean set the contents of memory to some predefined bit pattern, then no, that is not necessary unless your algorithm does evil things with uninitialized data.
    - Alon

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Memory clear for time testing in c++

    Why don't you start explaining what are you talking about: what algorithm, what "different constants values", etc. Why don't you post our code here, perhaps?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jun 2008
    Posts
    23

    Re: Memory clear for time testing in c++

    What I mean is I know if some data is stored in memory even after termination. If I run same thing again because some data is stored, second running is faster. My algorithm is
    By the way are there more precise measure of time than using clock_per_sec which is 1/100 precise.


    Code:
    #include <iostream.h>  // I/O 
    #include <fstream.h>   // file I/O
    #include <iomanip>   // format manipulation
    #include <vector>
    #include <string>
    
    #include <math.h>
    #include <conio.h>
    
    #include <stdio.h>
    
    
    const double delta_t_s=delta_t;
    const double force_t=0.1;
    
    const int No=100;
    const double e= 6.6438561898/(No*3.1415926535897932384626433832795);
    const double tau=0.00002;
    const double delta_t=0.21698;
    
    
    void mat_sum(std::vector<double>&, std::vector<double>&,std::vector<double>&);
    void mat_min(std::vector<double>&, std::vector<double>&,std::vector<double>&);
    void mat_mul(std::vector<double>&, std::vector<double>&,std::vector<double>&);
    void scal_mul(std::vector<double>&, std::vector<double>&,double);
    void project(std::vector<double>&, std::vector<double>&);
    void circle(std::vector<double>&,int,int,double);
    void force(std::vector<double>&,int ,int);
    void gen_a(std::vector<double>&, std::vector<double>&);
    
    
    
    
    inline double getValue(std::vector<double>& vec,size_t xPos, size_t yPos)
      {
        return vec[yPos*No+xPos];
      }
    
    inline void setValue(std::vector<double>& vec, size_t xPos, size_t yPos, double value)
      {
        vec[yPos*No+xPos] = value;
      }
    
    
    std::vector<double> temp(No*No);
    std::vector<double> temp2(No*No);
    std::vector<double> temp3(No*No);
    std::vector<double> temp4(No*No);
    std::vector<double> force_m(No*No);
    std::vector<double> A(No*No);
    std::vector<double> result_seq(No*No);
    std::vector<double> middle_res(No*No);
    
    
    double int2world(int x) {
      return    x / (double) No;
    }
    int i,j;
    int main(){
    
    
    	///////initial data generation////////////////////////////////////////////////
      	 for(i=0;i<No;i++){
    	   for(j=0;j<No;j++){
    	    circle(result_seq,i,j,radius);
    	   }
    	  }
    
    	const double seq=radius*radius/(2*delta_t);
    
    	double T=radius*radius/2;
    	
    
    	for(i=0;i<No;i++){
    	 for(j=0;j<No;j++){
    		force(force_m,i,j);
    	 }
    	}
    	
    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    	clock_t start,finish; 
    	double t=0;
    	for (int s=0;s<;s++)
    
    	{
    		double current_r=sqrt(radius*radius-2*time);
    
    		start = clock();
    		gen_a(A,result_seq);
    		scal_mul(result_seq,temp,1.0+tau/(e*e));
    		scal_mul(A,temp2,tau);
    		mat_min(temp,temp2,temp3);
    		mat_sum(temp3,force_m,temp4);
    		project(temp4,result_seq);
    		finish = clock();
    		temp_time = (double(finish)-double(start))/CLOCKS_PER_SEC;
    		cout<<"T="<<time<<endl;   
    
    }
    
    
    double abs(double v){
    
    if (v>=0)
    {
    	return v;
    }
    else{
    	return -v;
    }
    }
    
    
    
    void circle(std::vector<double>& vec,int x,int y,double r){
    	double norm_x=sqrt((centre_x-x)*(centre_x-x)+(centre_y-y)*(centre_y-y));
    
    	if (norm_x<=r-e*3.14/2)
    		{
    		setValue(vec,x,y,1.0);
    		}
    
    	else if (abs(r-norm_x)<e*3.14/2)
    		{
    		setValue(vec,x,y,sin((r-norm_x)/e));
    		}
    
    	else{
    		setValue(vec,x,y,-1.0);
    		}
    }
    
    
    
    void force(std::vector<double>& vec,int x,int y){
    	double test2_r=No/4;
    	int test=(No-test2_r-x)*(No-test2_r-x)+(No-test2_r-y)*(No-test2_r-y);
    
    	if (test<=radius2*radius2){
    	setValue(vec,i,j,force_t);
    	}
    	else{
    	setValue(vec,i,j,0.0);
    	}
    
    }
    
    
    
    
    
    
    void project(std::vector<double>& vec_A, std::vector<double>& vec_C){
    	double k;
    	for( int i=0;i<No;i++){
    	 for ( int j=0;j<No;j++){
    
    	  if (1.0<=getValue(vec_A,i,j))
    	   { k=1.0;  }
    	  else
    	   { k=getValue(vec_A,i,j); }
    
    	  if (k>=-1.0){
    		setValue(vec_C,i,j,k);}
    	  else{
    		setValue(vec_C,i,j,-1.0);}
    	 }
    	}
    }
    
    
    
    
    void mat_sum (std::vector<double>& A,std::vector<double>& B,std::vector<double>& C){
    	double a,b;
    	for (int i=0;i<No;i++){
    	  for (int j=0;j<No;j++)
    		  {
    		  a=getValue(A,i,j);
    		  b=getValue(B,i,j);
    		  setValue(C,i,j,a+b);
    //		  printf("%2.2f ",getValue(C,i,j));
    	      }
    	}
    }
    
    void mat_min (std::vector<double>& A, std::vector<double>& B,std::vector<double>& C){
    	double a,b;
        for (int i=0;i<No;i++){
    	   for (int j=0;j<No;j++){
    		a=getValue(A,i,j);
    		b=getValue(B,i,j);
    		setValue(C,i,j,a-b);
    //		printf("%2.2f ",getValue(C,i,j));
       }}
    }
    
    
    
    void mat_mul (std::vector<double>& A, std::vector<double>& B,std::vector<double>& C){
    double a,b;
    double c=0.0;
     for (int i=0;i<No;i++){
      for (int j=0;j<No;j++){
    
        setValue(C,i,j,0.0);
        for (int k=0;k<No;k++){
          a=getValue(A,i,k);
          b=getValue(B,k,j);
          c+=a*b;
        }
        setValue(C,i,j,c);
        c=0.0;
    
      }
     }
    }
    
    void scal_mul (std::vector<double>& A, std::vector<double>& C,double m){
    double a;
    for(int i=0;i<No;i++){
     for(int j=0;j<No;j++){
      a=getValue(A,i,j);
      setValue(C,i,j,a*m);
      }}
    }
    
    
    
    
    void gen_a(std::vector<double>& A, std::vector<double>& result_seq){
    	double k=1.0/(No*No);
    	for(int  i=0;i<No;i++){
    	 for (int j=0;j<No;j++){
    		 double i_plus_one=getValue(result_seq,i+1,j);
    		 double i_minus_one=getValue(result_seq,i-1,j);
    		 double i_j=getValue(result_seq,i,j);
    		 double j_plus_one=getValue(result_seq,i,j+1);
    		 double j_minus_one=getValue(result_seq,i,j-1);
    
    		 if(i==0 && j==0){
    
    		setValue(A,i,j,(-2.0*i_plus_one+4.0*i_j-2.0*j_plus_one)/k);
    		  }
    
    		else if(i==0 && j==No-1){
    		setValue(A,i,j,(-2.0*i_plus_one+4.0*i_j-2.0*j_minus_one)/k);
    
    		 
    		// printf("case 2 \n");
    		}
    
    		 else if(i==No-1 && j==0){
    		setValue(A,i,j,(-2.0*i_minus_one+4.0*i_j-2.0*j_plus_one)/k);
    
    		// printf("case 3 \n");
    		}
    
    		 else if(i==No-1 && j==No-1){
    		setValue(A,i,j,(-2.0*i_minus_one+4.0*i_j-2.0*j_minus_one)/k);
    
    		}
    
    
    		 else if(i==0 && j!=0 && j!=No-1){
    		setValue(A,i,j,(-2.0*i_plus_one+4.0*i_j-j_minus_one-j_plus_one)/k);
    
    		}
    
    		else if(i==No-1 && j!=0 && j!=No-1){
    		setValue(A,i,j,(-2.0*i_minus_one+4.0*i_j-j_plus_one-j_minus_one)/k);
    
    		}
    
    		else if(j==0 && i!=0 && i!=No-1){
    		setValue(A,i,j,(-i_plus_one-i_minus_one+4.0*i_j-2.0*j_plus_one)/k);
    
    		}
    
    
    		else if(j==No-1 && i!=0 && i!=No-1){
    		setValue(A,i,j,(-i_plus_one-i_minus_one+4.0*i_j-2.0*j_minus_one)/k);
    
    		}
    
    		else{
    		setValue(A,i,j,(-i_plus_one-i_minus_one+4.0*i_j-j_plus_one-j_minus_one)/k);
    		}
    
        }
        }
    }

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Memory clear for time testing in c++

    Take a look at this FAQ for precise time measuring: http://www.codeguru.com/forum/showthread.php?t=291294.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured