直接从 COLEDateTime 获取年份和月份还是先转换为 SYSTEMTIME 更快
Is it faster to get year and month directly from COleDateTime or converting to SYSTEMTIME first?
我想从 COLEDateTime 对象获取年份和月份,我希望它尽可能快。我有 2 个选择;
COleDateTime my_date_time;
int year = my_date_time.GetYear();
int month = my_date_time.GetMonth();
或
COleDateTime my_date_time;
SYSTEMTIME mydt_as_systemtime;
my_date_time.GetAsSystemTime(mydt_as_systemtime);
int year = mydt_as_systemtime.wYear;
int month = mydt_as_systemtime.wMonth;
问题是,哪个会更快?
COleDateTime
将其内部日期表示形式存储为 DATE
typedef,因此当您调用 GetYear()
并GetMonth()
时,它每次都必须计算这些。在SYSTEMTIME
情况下,wYear
和 wMonth
的值存储为 DWORD
s,因此这只是检索值的情况,但将COleDateTime
转换为SYSTEMTIME
会产生开销。
谢谢
斯特伦
多亏了@MarkRansom正确的方向,我找到了COLEDateTime的源代码。以下是功能;
ATLCOMTIME_INLINE int COleDateTime::GetYear() const throw()
{
SYSTEMTIME st;
return GetAsSystemTime(st) ? st.wYear : error;
}
ATLCOMTIME_INLINE int COleDateTime::GetMonth() const throw()
{
SYSTEMTIME st;
return GetAsSystemTime(st) ? st.wMonth : error;
}
所以COleDateTime::GetYear()
无论如何::GetMonth()
转换为SYSTEMTIME
!
由于这些是内联函数,因此这些将在呼叫站点就位。由于这些函数之间的GetAsSystemTime(st)
是通用的,编译器优化应该将其分解为一个临时变量,因此我的 qestion 中的两个代码片段是等效的。由于选项 1 更简单,因此没有理由不这样做。
更新:
一旦我有机会,我就对代码进行了基准测试。看起来我所说的编译器优化不适用于上述代码。这两种方法的 100 万次操作的计时如下;
直拨:154ms
系统时间方法:75ms
好吧,这就解决了。转换为SYSTEMTIME
它。
相关文章:
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 二叉搜索如何比线性搜索更快?
- push_back并插入 C++ STL 中哪个更快?
- 如何使插入排序更快?
- C++,为什么数组比矢量更快,使用更少的内存
- 哪个更快:在 1d 向量中按字符串搜索还是在 2d 向量中按向量搜索?
- 如何使科学记数法的转换更精确?
- 哪种方式更快?究竟发生了什么,我们没有看到什么?
- 使用 int32_t 而不是双精度运行矢量点积是否更快?
- 更快地将卤化物图像转换为 opencv 垫?
- 更快的方法将数字从范围转换为另一个范围
- 如何让 GCC 编译器将变量划分转换为 mul(如果更快)
- 在科学记数法中将双精度转换为字符串的速度比在 c++ 中将 sprintf 转换得更快
- 为什么将十进制转换为二进制的递归方法比迭代、使用和返回字符串更快
- 直接从 COLEDateTime 获取年份和月份还是先转换为 SYSTEMTIME 更快
- 将字符串转换为数字,以便更快地进行比较
- c++中最有效的将string转换为int的方法(比atoi更快)
- 需要一个更快的方式来转换cv::Mat成一维矢量形式
- 大多数编译器将% 2转换为位比较吗?真的更快吗?
- 转换位数组以更快地设置