#include <time.h>
    #include <sys/time.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <cctype>
    #include <algorithm>
    
    /* Return 1 if the difference is negative, otherwise 0.  */
    int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
    {
    	long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
    	result->tv_sec = diff / 1000000;
    	result->tv_usec = diff % 1000000;
    	
    	return (diff<0);
    }
    
    void timeval_print(struct timeval *tv)
    {
    	char buffer[30];
    	time_t curtime;
    	
    	printf("%ld.%06ld", tv->tv_sec, tv->tv_usec);
    	curtime = tv->tv_sec;
    	strftime(buffer, 30, "%m-%d-%Y  %T", localtime(&curtime));
    	printf(" = %s.%06ld\n", buffer, tv->tv_usec);
    }
    
    int main()
    {
    	struct timeval tvBegin, tvEnd, tvDiff;
    	
    	// begin
    	gettimeofday(&tvBegin, NULL);
    	timeval_print(&tvBegin);

        static char str1[100] = "str,, ing";
        size_t size = sizeof(str1)/sizeof(str1[0]);
        
    	// lengthy operation
    	int i,j;
    	for(i=0;i<999999L;++i) {
            int bad = 0;
            int cur = 0;
            while (str1[cur] != '\0') {
                if (bad < cur && !ispunct(str1[cur]) && !isspace(str1[cur])) {
                    str1[bad] = str1[cur];
                }
                if (ispunct(str1[cur]) || isspace(str1[cur])) {
                    cur++;
                }
                else {
                    cur++;
                    bad++;
                }
            }
            str1[bad] = '\0';
    	}
    	
    	//end
    	gettimeofday(&tvEnd, NULL);
    	timeval_print(&tvEnd);
    	
    	// diff
    	timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
    	printf("%ld.%06ld\n", tvDiff.tv_sec, tvDiff.tv_usec);
    
    	return 0;
    }
