Measure time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kartouss
    New Member
    • Feb 2008
    • 1

    Measure time

    Hello i am new to this forum...can you help me solve this problem..
    I have a function encrypt() which encrypts data from a file in 16 bytes till end of file which in turn calls other 4 functions...and i need to measure the time it encrypts the data consecutively for all the 4 functions till end of file...
    I am fwding you the codes...Please help me out...

    Code:
    int Encrypt (word8 a[4][MAXBC], word8 rk[MAXROUNDS+1][4][MAXBC])
    {	// Encryption of one blockl
    		int r;
    	sec_init();
    	start_time = sec();
    	AddRoundKey(a, rk[0]);
    	stop_time = sec();
    	Add1 = (stop_time - start_time);
    	
    	for ( r=1; r < ROUNDS; r++)
    	{
    		sec_init();
    		start_time = sec();
    			SubBytes (a,S);
    		stop_time = sec();
    		 sub1 = (stop_time - start_time);
    		 
    		sec_init();
    		start_time = sec();
    			ShiftRows(a);
    		stop_time = sec();
    		sft1 = (stop_time - start_time);
    		
    		sec_init();
    		start_time = sec();			
    			MixColumns(a);
    		stop_time = sec();
    		mix1 = (stop_time - start_time);
    		
    		sec_init();
    		start_time = sec();
    			AddRoundKey(a,rk[r]);
    		stop_time = sec();
    		Add2 = (stop_time - start_time);		
    	}
    	/* Last round is special; there is no MixColumns
    	*/
    	sec_init();
    	start_time = sec();		
    		SubBytes (a,S);
    	stop_time = sec();
    	sub2 = (stop_time - start_time);
    
    	sec_init();
    	start_time = sec();		
    		ShiftRows(a);
    	stop_time = sec();
    	sft2 = (stop_time - start_time);
    
    	sec_init();
    	start_time = sec();		
    		AddRoundKey(a,rk[ROUNDS]);
    	stop_time = sec();
    	Add3 = (stop_time - start_time);
    
    	return 0;
    }
    // main function

    // opens up a file plaintext.txt

    Code:
    char d;
    	ifstream myfile1 ("Plaintext.txt");
    	ofstream myfile3 ("Ciphertext.txt");
    	
    	myfile1.get(d);  //priming read
    
      while(!myfile1.eof())
    {
    	  for ( j=0; j<BC; j++)
    	  for ( i=0; i < 4; i++)        
    	  {
    		  a[i][j] = d; // plaintext
    		  //cout<<a[i][j]; // to chk how many plaintext is copied
    		  myfile1.get(d);  //reads first element of next block
    	  }
    
    	  //sec_init();
    		//start_time = sec();
    
    		  Encrypt(a, rk);
    
    		//stop_time = sec();
    	    //enTime = (stop_time - start_time);
    		//sum2+=enTime;
    
    	  if (myfile3.is_open())
    	  {
    		  for(j=0; j< BC; j ++)
    			  for ( i=0; i<4; i++)
    				  myfile3<<a[i][j];
    	  }   
    	  else        
    		cout << "Unable to open file";
      }					
    	 
      	myfile3.close();
    	myfile1.close();
    // measure performance...

    Code:
    Add = Add1 + Add2*(ROUNDS-1) + Add3;
    		sub = sub1*(ROUNDS-1) + sub2;
    		sft = sft1*(ROUNDS-1) + sft2;
    		mix = mix1*(ROUNDS-1);
    
    		encryptTime = Add + sub + sft + mix;
    The problem is that i am able to record the time for the first 16 bytes.. and i need to record the time for encrypting the data in the plaintext.txt file till eof()
    please help me out..
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I dont know how the sec() function works
    But my idea would be use time_t structure and use this logic

    [code=c]
    time_t start,end;
    time(&start);
    <your function call>
    time(&end);

    difftime(end,st art);//this gives the time difference in seconds

    [/code]

    Thanks
    Raghuram

    Comment

    Working...