Timestamp测试

来源:互联网 发布:java游戏小程序 编辑:程序博客网 时间:2024/06/05 03:30

参考:muduo库以及http://blog.csdn.net/ygm_linux/article/details/25690829

Timestamp.h

#ifndef MUDUO_BASE_TIMESTAMP_H
#define MUDUO_BASE_TIMESTAMP_H
#include <boost/operators.hpp>
#include <iostream>
#include <string>


using namespace std;


namespace muduo
{
class copyable
{
};


class Timestamp : public copyable,
                 public boost::less_than_comparable<Timestamp>
{
public:
 ///
 /// Constucts an invalid Timestamp.
 ///
 Timestamp()
   : microSecondsSinceEpoch_(0)
 {
 }


 ///
 /// Constucts a Timestamp at specific time
 ///
 /// @param microSecondsSinceEpoch
 explicit Timestamp(int64_t microSecondsSinceEpoch);


 void swap(Timestamp& that)
 {
   std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
 }


 // default copy/assignment/dtor are Okay


 string toString() const;
 string toFormattedString() const;


 bool valid() const { return microSecondsSinceEpoch_ > 0; }


 // for internal usage.
 int64_t microSecondsSinceEpoch() const { return microSecondsSinceEpoch_; }
 time_t secondsSinceEpoch() const
 { return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); }


 ///
 /// Get time of now.
 ///
 static Timestamp now();
 static Timestamp invalid();


 static const int kMicroSecondsPerSecond = 1000 * 1000;


private:
 int64_t microSecondsSinceEpoch_;
};


inline bool operator<(Timestamp lhs, Timestamp rhs)
{
 return lhs.microSecondsSinceEpoch() < rhs.microSecondsSinceEpoch();
}


inline bool operator==(Timestamp lhs, Timestamp rhs)
{
 return lhs.microSecondsSinceEpoch() == rhs.microSecondsSinceEpoch();
}


///
/// Gets time difference of two timestamps, result in seconds.
///
/// @param high, low
/// @return (high-low) in seconds
/// @c double has 52-bit precision, enough for one-microseciond
/// resolution for next 100 years.
inline double timeDifference(Timestamp high, Timestamp low)
{
 int64_t diff = high.microSecondsSinceEpoch() - low.microSecondsSinceEpoch();
 return static_cast<double>(diff) / Timestamp::kMicroSecondsPerSecond;
}


///
/// Add @c seconds to given timestamp.
///
/// @return timestamp+seconds as Timestamp
///
inline Timestamp addTime(Timestamp timestamp, double seconds)
{
 int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
 return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
}


}


#endif


Timestamp.cpp

#include <boost/static_assert.hpp>
#include <sys/time.h>
#include <stdio.h>
#include "Timestamp.h"


/*
http://blog.csdn.net/ygm_linux/article/details/25899385
跨平台的:64位整数

*/
#define __STDC_FORMAT_MACROS
#include <inttypes.h> //PRId64
#undef __STDC_FORMAT_MACROS


using namespace muduo;


BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));




/*
以微秒初始化microSecondsSinceEpoch_
*/
Timestamp::Timestamp(int64_t microseconds)
  : microSecondsSinceEpoch_(microseconds)
{
}




/*
将微秒microSecondsSinceEpoch_ 转换为[秒seconds +  微秒microseconds]
*/
string Timestamp::toString() const
{
  char buf[32] = {0};
  int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;
  int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;
  snprintf(buf, sizeof(buf)-1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
  return buf;
}




/*
将微秒microSecondsSinceEpoch_ 转换为[time_t]
http://blog.csdn.net/csuwzc/article/details/6912104
再利用gmtime_r 将time_t 时间做转换存于 tm_time
*/
string Timestamp::toFormattedString() const
{
  char buf[32] = {0};
  time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
  int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
  struct tm tm_time;
  gmtime_r(&seconds, &tm_time);


  snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
      tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
      tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
      microseconds);
  return buf;
}




/*
将当前时间以微秒存Timestamp类的变量microSecondsSinceEpoch 中;
调用explicit Timestamp(int64_t) 返回
*/
Timestamp Timestamp::now()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  int64_t seconds = tv.tv_sec;
  return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}




/*
将0 存Timestamp类的变量microSecondsSinceEpoch 中;
调用Timestamp() 返回
*/
Timestamp Timestamp::invalid()
{
  return Timestamp();
}


test.cpp

#include <vector>
#include <stdio.h>
#include "Timestamp.h"


using muduo::Timestamp;




void benchmark()
{
/*获得1000*1000次时间存于vector 中*/
 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()));
         printf("===================\n");


/*
start为上次时间的微妙数,
next为下次时间的微妙数,
inc 表示时间差next - start;
统计前后时间差为index (<100微妙)出现的次数
increments[index]

*/
 int increments[100] = { 0 };
 int64_t start = stamps.front().microSecondsSinceEpoch();
 for (int i = 1; i < kNumber; ++i)
 {
   int64_t next = stamps[i].microSecondsSinceEpoch();
   int64_t inc = next - start;
   start = next;

   if (inc < 0)
   {
     printf("reverse!\n");
   }
   else if (inc < 100)
   {
     ++increments[inc];
   }
   else
   {
     printf("big gap %d\n", static_cast<int>(inc));
   }
 }


/*获得的1000*1000 次时间,前后的时间差值为inc 微妙,
increments[inc] 即为出现inc< 100 的次数

*/
 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());
  printf("===================\n");


  /*统计可知基本获得的连续的时间差在100微妙以内*/
  benchmark();
}


0 0