fork download
  1. #include <iostream>
  2. #include <sstream>
  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() {
  41. // your code goes here
  42. TscMeasurement ttime;
  43. volatile long long i;
  44. std::stringbuf::pos_type begin_pos(0);
  45. std::stringbuf buffer(std::ios_base::out);
  46. std::ostream os (&buffer);
  47.  
  48. tsc_start_measurement(&ttime);
  49. for(i=0; i < 10000000UL; i++) {
  50. buffer.pubseekpos(begin_pos, std::ios_base::out);
  51. os << i << " " << "bla-bla-bla" << -10 << 1.3f << std::endl;
  52. }
  53. tsc_stop_measurement(&ttime);
  54.  
  55. printf ("Time: %llu\n", tsc_get_ticks(&ttime) );
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 3.26s 16064KB
stdin
Standard input is empty
stdout
Time: 9785048702