加速类构造函数C ++

时间:2016-05-21 18:20:36

标签: c++ c++11

所以我正在为我编写的系统进行速度测试。为此,我编写了自己的DateTime命名空间,因为我需要比ctime更多的控制权给我,并且不希望受限于系统时间。在我的测试中,我发现DateTimeParse和TimeZoneSpec类的构造函数正在减慢DateTime对象的构造,即使它大致需要3.4E-5秒这样做200k就可以了。

有两个构造函数需要大约相同的时间。第一个创建格式说明符的映射,类似于存储其边界和类型的strtime格式(即%z,%Y)以及使用std :: function将getter和setter(在适用中)绑定到正确的函数。

DateTimeParse::DateTimeParse()
{
  m_map.insert(std::unordered_map<std::string,DateTimeFormat>::value_type("Y",DateTimeFormat('Y',4,"int",1970,3000,std::bind(&DateTimeParse::_set_year, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_year, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("m",DateTimeFormat('m',2,"int",1,12,std::bind(&DateTimeParse::_set_month, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_month, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("d",DateTimeFormat('d',2,"int",1,31,std::bind(&DateTimeParse::_set_day, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_day, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("H",DateTimeFormat('H',2,"int",0,23,std::bind(&DateTimeParse::_set_hour, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_hour, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("M",DateTimeFormat('M',2,"int",0,59,std::bind(&DateTimeParse::_set_min, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_min, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("S",DateTimeFormat('S',2,"int",0,59,std::bind(&DateTimeParse::_set_sec, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_sec, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("Z",DateTimeFormat('Z',3,"string",0,0,std::bind(&DateTimeParse::_set_tz, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_tz, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("a",DateTimeFormat('a',3,"string",0,0, std::bind(&DateTimeParse::_get_weekday_abbr, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("A",DateTimeFormat('A',0,"string",0,0, std::bind(&DateTimeParse::_get_weekday_full, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("b",DateTimeFormat('b',3,"string",0,0, std::bind(&DateTimeParse::_get_month_abbr, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("B",DateTimeFormat('B',0,"string",0,0, std::bind(&DateTimeParse::_get_month_full, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("j",DateTimeFormat('j',3,"string",1,366,std::bind(&DateTimeParse::_set_julian, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_julian, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("z",DateTimeFormat('z',3,"string",0,99,std::bind(&DateTimeParse::_set_tz_with_offset, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_tz_with_offset, this, std::placeholders::_1))));
  m_map.insert(std::unordered_map<std::string, DateTimeFormat>::value_type("s",DateTimeFormat('s',10,"ulint",static_cast<unsigned long int>(0),static_cast<unsigned long int>(4294967295),std::bind(&DateTimeParse::_set_epoch, this, std::placeholders::_1, std::placeholders::_2), std::bind(&DateTimeParse::_get_epoch, this, std::placeholders::_1))));
};

以上内容存储在std :: map。

第二个构造函数创建一个std :: vector&gt;包含时区规范。

TimeZoneSpec::TimeZoneSpec()
{
  std::vector<std::string> tz;
  std::vector<std::vector<std::string> > tzdb;

  tz = {"Greenwich, London","UTC","Coordinated Universal Time","","","-00:00:00","","","","",""};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"Greenwich, London","UTC","Coordinated Universal Time","","","+00:00:00","","","","",""};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"Greenwich, London","GMT","Greenwich Mean Time","","","00:00:00","","","","",""};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/New_York","EST","Eastern Standard Time","EDT","Eastern Daylight Time","-05:00:00","+01:00:00","2;0;3","+02:00:00","1;0;11","+02:00:00"};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/Chicago","CST","Central Standard Time","CDT","Central Daylight Time","-06:00:00","+01:00:00","2;0;3","+02:00:00","1;0;11","+02:00:00"};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/Denver","MST","Mountain Standard Time","MDT","Mountain Daylight Time","-07:00:00","+01:00:00","2;0;3","+02:00:00","1;0;11","+02:00:00"};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/Los_Angeles","PST","Pacific Standard Time","PDT","Pacific Daylight Time","-08:00:00","+01:00:00","2;0;3","+02:00:00","1;0;11","+02:00:00"};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/Anchorage","AKST","AKST","AKDT","AKDT","-09:00:00","+01:00:00","2;0;3","+02:00:00","1;0;11","+02:00:00"};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/Adak","HAST","HAST","HADT","HADT","-10:00:00","+01:00:00","2;0;3","+02:00:00","1;0;11","+02:00:00"};
  tzdb.push_back(tz);
  tz.clear();
  tz = {"America/Phoenix","MST","Mountain Standard Time","","","-07:00:00","+00:00:00","","","","+00:00:00"};
  tzdb.push_back(tz);
  tz.clear();

  m_tzdb = tzdb;
  };

我想知道是否有人有任何想法加快这一点。他们再次花费的时间很短,但当你做到200K +次时,它就开始加起来。谢谢你的帮助。

0 个答案:

没有答案