1. 程式人生 > >【Muduo庫】【base】一、Timestamp類

【Muduo庫】【base】一、Timestamp類

second 一個 macro fin ftime mac cat gap base

一、Timestamp類

1、類圖如下:

技術分享

2、 知識點

(1) 這個類繼承了 muduo::copyable, 以及 boost::less_than_comparable.

(2) boost::less_than_comparable 這個類要求實現 <, 可以自動實現 >, <=, >= (自動推導出來的,模板元的思想)

3、 學習流程
(1) 剝離代碼 (在 ~/muduo_practice 下), 編譯出來一個只有TimeStamp的base庫
(2) 小的測試程序在 ~/muduo_practice/tests 下
    a. Bsa.cc 測試boost編譯時斷言的 -> BOOST_STATIC_ASSERT

      

技術分享
 1 #include <boost/static_assert.hpp>
 2 
 3 class Timestamp
 4 {
 5 private:
 6         int64_t microseconds; 
 7 };
 8 
 9 BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
10 //BOOST_STATIC_ASSERT(sizeof(short) == sizeof(int64_t));
11 int main() {
12         return 0;
13 }
bsa.cc

    b. 測試PRId64

技術分享
 1 #include <cstdio>
 2 #include <ctime>
 3 #define _STDC_FORMAT_MACROS
 4 #include <inttypes.h>
 5 #undef _STDC_FORMAT_MACROS
 6 
 7 int main(void) {
 8         int64_t value = time(NULL);
 9         printf("%"PRId64"\n", value);
10         return 0;
11 }
prid.cc

    c. 設計一個合理的對TimeStamp類的benchmark
    用一個vector數組存儲Timestamp, 用Timestamp的微秒表示計算相鄰兩個時間戳的時間差。Vector需要提前reserve空間,避免內存開銷影響benchmark。
    代碼見Timestamp_sara.cc

技術分享
 1 //2017-10-29
 2 //Add by wyzhang
 3 //Learn Muduo -- test Timestamp
 4 
 5 #include <muduo/base/Timestamp.h>
 6 #include <cstdio>
 7 #include <vector>
 8 #define _STDC_FROMAT_MACROS
 9 #include <inttypes.h>
10 #undef _STDC_FORMAT_MACROS
11 
12 using namespace muduo;
13 
14 // design a benchmark function to test class Timestamp
15 // we can use a vector to record Timestamp, and calculate difference of neighbors
16 void benchmark() {
17     const int kNumbers = 1000 * 1000;
18     std::vector<Timestamp> vecTimestamp;
19     vecTimestamp.reserve(kNumbers);  //must preReserve. in case calculate the time of allocate mem
20     for (int i = 0; i < kNumbers ; ++i ) {
21         vecTimestamp.push_back(Timestamp::now());
22     }
23 
24     int gap[100] = {0};
25     Timestamp start = vecTimestamp.front();
26     for (int i = 1; i < kNumbers; ++i ) {
27         Timestamp next = vecTimestamp[i];
28         int64_t calGap = next.microSecondsSinceEpoch() - start.microSecondsSinceEpoch();  // use microSeconds here
29         start = next;
30         if(calGap < 0) {
31             printf("calGap < 0\n");
32         } else if (calGap < 100){
33             gap[calGap]++;
34         } else {
35             printf("bigGap. [%"PRId64"]\n", calGap);
36         }
37     }
38     for (int i = 0; i < 100; ++i) {
39         printf("%d: %d\n", i, gap[i]);
40     }
41 }
42 
43 
44 int main() {
45     //[1] test print timestamp
46     Timestamp ts(Timestamp::now());
47     printf("print now = %s\n", ts.toString().c_str());
48     sleep(1);
49     Timestamp ts2 = Timestamp::now();
50     printf("ts2 = %s\n", ts2.toString().c_str());
51     double difftime = timeDifference(ts2, ts);
52     printf("difftime: %f\n", difftime);
53     //[2] run benchmark
54     benchmark();
55     return 0;
56 }
timestamp_sara.cc

執行結果截圖如下:沒有截全

技術分享

【Muduo庫】【base】一、Timestamp類