Calculate the elapsed time

Windows SDK uses DWORD GetTickCount() functions to calculate a elapsed time between 2 points. DWORD start = GetTickCount(); //Do some work Sleep( 10 ); DWORD end = GetTickCount(); DWORD elapsed = end - start; Linux way of calculating Using gettimeofday function is the most general method. #include <stdio.h> // for printf() #include <sys/time.h> // for clock_gettime() #include <unistd.h> // for usleep() int main () { struct timeval start, end; long secs_used,micros_used; gettimeofday( & start, NULL); usleep( 1250000 ); // Do the stuff you want to time here gettimeofday( & end, NULL); printf( "start: %ld secs, %ld usecs \n " ,start.tv_sec,start.tv_usec); printf( "end: %ld secs, %ld usecs \n " ,end.tv_sec,end.tv_usec); secs_used = (end.tv_sec - start.tv_sec); //avoid overflow by subtracting first micros_used = ((secs_used * 1000000 ) + end.tv_usec)...

Use getopt() function in C/C++ to parse command line arguments

The getopt() function is a builtin function in C and is used to parse command line arguments. Just include <unistd.h> Usage 1 Let's suppose you want to pass -h, -v, -f filename parameters to the program. #include <stdio.h> #include <unistd.h> int main ( int argc, char * argv[]) { int opt; while ((opt = getopt(argc, argv, "hvf:" )) != - 1 ) { switch (opt) { case 'h' : printf( "option: h is set \n " ); break ; case 'v' : printf( "option: v is set \n " ); break ; case 'f' : printf( "option h filename: %s \n " , optarg); break ; } } return 0 ; } Options that require additional parameters such as file name, add a colon after the opti...