start.pthread.m.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include <math.h>
  7. #include <pthread.h>
  8. #define OVERHEAD_TIMES 30000
  9. double my_sqrt(double num)
  10. {
  11. return sqrt(num);
  12. }
  13. pthread_t my_pthread_self(void)
  14. {
  15. return pthread_self();
  16. }
  17. int main (int argc, char ** argv, char ** envp)
  18. {
  19. struct timeval tv;
  20. gettimeofday(&tv, NULL);
  21. if (argc < 2)
  22. return -1;
  23. unsigned long long msec1 = atoll(argv[1]);
  24. unsigned long long msec2 = tv.tv_sec * 1000000ULL + tv.tv_usec;
  25. struct timeval tv1, tv2;
  26. gettimeofday(&tv1, NULL);
  27. for (int j = 0 ; j < OVERHEAD_TIMES ; j++)
  28. gettimeofday(&tv, NULL);
  29. gettimeofday(&tv2, NULL);
  30. unsigned long long msec3 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
  31. unsigned long long msec4 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
  32. unsigned long long overhead = (msec4 - msec3) / OVERHEAD_TIMES;
  33. printf("%llu\n", msec2 - msec1 - overhead);
  34. my_sqrt(1.0);
  35. my_sqrt(2.0);
  36. my_pthread_self();
  37. return 0;
  38. }