Permalink
Browse files

add benchmark for Timestamp class.

  • Loading branch information...
1 parent 1ae58bf commit 34cab01c891195c0b580b1586700d9877867bbb8 @chenshuo committed Jan 10, 2012
Showing with 43 additions and 0 deletions.
  1. +43 −0 datetime/Timestamp_unittest.cc
@@ -1,4 +1,5 @@
#include "Timestamp.h"
+#include <vector>
#include <stdio.h>
using muduo::Timestamp;
@@ -13,11 +14,53 @@ void passByValue(Timestamp x)
printf("%s\n", x.toString().c_str());
}
+void benchmark()
+{
+ const int kNumber = 1000*1000;
+
+ std::vector<Timestamp> stamps;
+ stamps.reserve(kNumber);
+ for (int i = 0; i < kNumber; ++i)
+ {
+ stamps.push_back(Timestamp::now());
+ }
+ printf("%s\n", stamps.front().toString().c_str());
+ printf("%s\n", stamps.back().toString().c_str());
+ printf("%f\n", timeDifference(stamps.back(), stamps.front()));
+
+ int increments[100] = { 0 };
+ int64_t start = stamps.front().microSecondsSinceEpoch();
+ for (int i = 1; i < kNumber; ++i)
+ {
+ int64_t next = stamps[i].microSecondsSinceEpoch();
+ int inc = next - start;
+ start = next;
+ if (inc < 0)
+ {
+ printf("reverse!\n");
+ }
+ else if (inc < 100)
+ {
+ ++increments[inc];
+ }
+ else
+ {
+ printf("big gap %d\n", inc);
+ }
+ }
+
+ for (int i = 0; i < 100; ++i)
+ {
+ printf("%2d: %d\n", i, increments[i]);
+ }
+}
+
int main()
{
Timestamp now(Timestamp::now());
printf("%s\n", now.toString().c_str());
// passByValue(now);
// passByConstReference(now);
+ benchmark();
}

0 comments on commit 34cab01

Please sign in to comment.