fork(2) download
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7.  
  8. typedef struct {
  9. long unsigned tsc_low,
  10. tsc_high,
  11. tsc_low1,
  12. tsc_high1;
  13. } TscMeasurement;
  14.  
  15. #define rdtsc(low,high) \
  16.   __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
  17.  
  18. inline void tsc_start_measurement (TscMeasurement *tm) {
  19. rdtsc (tm->tsc_low, tm->tsc_high);
  20. }
  21.  
  22. inline void tsc_stop_measurement (TscMeasurement *tm) {
  23. rdtsc (tm->tsc_low1, tm->tsc_high1);
  24. }
  25.  
  26. inline long long unsigned tsc_get_ticks (const TscMeasurement *tm)
  27. {
  28. long long unsigned tsc, tsc1;
  29. tsc = (long long unsigned) tm->tsc_high << 32 |
  30. ((long long unsigned) tm->tsc_low & 0xffffffff);
  31. tsc1 = (long long unsigned) tm->tsc_high1 << 32 |
  32. ((long long unsigned) tm->tsc_low1 & 0xffffffff);
  33. return tsc1 - tsc;
  34. }
  35.  
  36. #ifdef __cplusplus
  37. }
  38. #endif
  39.  
  40. int main(void) {
  41. // your code goes here
  42. TscMeasurement ttime;
  43. volatile long long i;
  44. char buf[100];
  45.  
  46. tsc_start_measurement(&ttime);
  47. for(i=0; i < 10000000UL; i++) {
  48. sprintf(buf, "%d %s%d%f\n", i,"bla-bla-bla", -10, 1.3);
  49. }
  50. tsc_stop_measurement(&ttime);
  51.  
  52. printf ("Time: %llu\n", tsc_get_ticks(&ttime) );
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 2.3s 9432KB
stdin
Standard input is empty
stdout
Time: 6942218607