/************************** File Header *************************************** * File Name : TC1_memV2.c * Author : Patni Computer Systems * Description : Allocation/deallocation of memory in a single thread *************************** End of File Header ********************************/ /************************* Include Section ************************************/ #include #include #include #include /*********************** End of Include Section *******************************/ /*********************** Functions Section ************************************/ /************************ Function Header ************************************** * Function Name : main * Description : Allocation/deallocation of memeory in a single thread * Input Values : int size , int count * Return Values : *******************************************************************************/ int main(int argc, char *argv[]) { int l_iIndex=0,l_iCount=0; size_t l_iSize=0; int l_iStartRet=0,l_iEndRet=0; char *l_pcTemp=NULL; double l_dUsecs=0; int j; char *pcTemp; struct timeval l_stTimeValStart,l_stTimeValEnd,l_stTimeValDiff; /* checking for argument count */ if(3 != argc) { printf("Usage : \n "); exit(1); } l_iSize = atoi(argv[1]); l_iCount = atoi(argv[2]); /* Marking the start time */ l_iStartRet = gettimeofday(&l_stTimeValStart,NULL); /* Allocating and Deallocating l_iSize bytes l_iCount times */ for(l_iIndex = 0; l_iIndex < l_iCount ; l_iIndex++) { l_pcTemp = (char *)malloc(l_iSize); if(NULL == l_pcTemp) { printf("Memory allocation failed \n"); exit(1); } // printf(" Start Address %p \n",l_pcTemp); // printf(" End Address %p \n",l_pcTemp+l_iSize-1); memset(l_pcTemp,0,l_iSize); free(l_pcTemp); } /* Marking End time */ l_iEndRet = gettimeofday(&l_stTimeValEnd,NULL); /* Checking gettimeofday return status */ if(0 != l_iStartRet ) { printf(" Marking start time failed \n"); exit(1); } if(0 != l_iEndRet) { printf(" Marking End time failed \n"); exit(1); } #ifdef DEBUG /*Printing Start and End time */ printf("Allocating and Deallocating %d Bytes %d times \n",l_iSize,l_iCount); printf("Start time : %u seconds %u useconds \n",l_stTimeValStart.tv_sec,l_stTimeValStart.tv_usec); printf("End time : %u seconds %u useconds \n",l_stTimeValEnd.tv_sec,l_stTimeValEnd.tv_usec); #endif /* Calculating the Difference */ timersub(&l_stTimeValEnd,&l_stTimeValStart,&l_stTimeValDiff); #ifdef DEBUG printf("%d Bytes of malloc time for %d no of loops = %u seconds %u useconds \n",l_iSize,l_iCount,l_stTimeValDiff.tv_sec,l_stTimeValDiff.tv_usec); #endif /* Calculation of Total time taken */ l_dUsecs = (l_stTimeValDiff.tv_sec * 1000000) + l_stTimeValDiff.tv_usec ; printf("Total time taken %f usecs \n",l_dUsecs); printf("Time for 1 loop %f usecs \n",l_dUsecs/l_iCount); return 0; } /******************* End of Functions Section ********************************/