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