fork download
  1. #include <time.h>
  2. #include <sys/time.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <cctype>
  7. #include <algorithm>
  8.  
  9. /* Return 1 if the difference is negative, otherwise 0. */
  10. int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
  11. {
  12. long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
  13. result->tv_sec = diff / 1000000;
  14. result->tv_usec = diff % 1000000;
  15.  
  16. return (diff<0);
  17. }
  18.  
  19. void timeval_print(struct timeval *tv)
  20. {
  21. char buffer[30];
  22. time_t curtime;
  23.  
  24. printf("%ld.%06ld", tv->tv_sec, tv->tv_usec);
  25. curtime = tv->tv_sec;
  26. strftime(buffer, 30, "%m-%d-%Y %T", localtime(&curtime));
  27. printf(" = %s.%06ld\n", buffer, tv->tv_usec);
  28. }
  29.  
  30. int main()
  31. {
  32. struct timeval tvBegin, tvEnd, tvDiff;
  33.  
  34. // begin
  35. gettimeofday(&tvBegin, NULL);
  36. timeval_print(&tvBegin);
  37.  
  38. static char str1[100] = "str,, ing";
  39. size_t size = sizeof(str1)/sizeof(str1[0]);
  40.  
  41. // lengthy operation
  42. int i,j;
  43. for(i=0;i<999999L;++i) {
  44. int bad = 0;
  45. int cur = 0;
  46. while (str1[cur] != '\0') {
  47. if (bad < cur && !ispunct(str1[cur]) && !isspace(str1[cur])) {
  48. str1[bad] = str1[cur];
  49. }
  50. if (ispunct(str1[cur]) || isspace(str1[cur])) {
  51. cur++;
  52. }
  53. else {
  54. cur++;
  55. bad++;
  56. }
  57. }
  58. str1[bad] = '\0';
  59. }
  60.  
  61. //end
  62. gettimeofday(&tvEnd, NULL);
  63. timeval_print(&tvEnd);
  64.  
  65. // diff
  66. timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
  67. printf("%ld.%06ld\n", tvDiff.tv_sec, tvDiff.tv_usec);
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0.1s 2860KB
stdin
Standard input is empty
stdout
1392335645.961366 = 02-13-2014  23:54:05.961366
1392335646.068226 = 02-13-2014  23:54:06.068226
0.106860