c++ - union 对象就像一个结构

标签 c++ windows performance winapi performancecounter

更新:

如果您想查看此问题的原始表现形式,请阅读“原始问题”部分。

简而言之:我正在修改 C++ union 对象的一个​​字段,但这对其余字段没有影响,它的行为非常像一个结构。解决方法:见我的回答。

原始问题

tl;dr: QueryPeformanceCounter 不应该在提供的 LONG_INTEGERQuadPart 字段中返回其值,而不是HighPart/LowPart?我在任何地方都找不到这是系统特定的,但似乎是这样。

详情

我从 Windows 的 QueryPeformanceCounter 得到了一个特殊的行为。考虑这个非常简单的用法,紧跟Microsoft's example :

#include <windows.h>

bool test()
{
    LARGE_INTEGER start, end, freq;
    if (!QueryPerformanceFrequency(&freq)) {
        cout << "QueryPerformanceFrequency failed!\n";
        return false;
    }
    QueryPerformanceCounter(&start);

    Sleep(1000);  // Simulate work

    QueryPerformanceCounter(&end);
    cout << "range: from " << start.QuadPart << " to " << end.QuadPart << endl;
    return true;
}

我得到以下输出:

range: from -3689348814741910324 to -3689348814741910324

这似乎很随机,但事实并非如此。添加转储功能:

ostream& operator << (ostream& os, const LARGE_INTEGER& li) {
return os << std::hex << std::setfill('0') << "["
    << "HP: 0x"   << std::setw( 8) << li.HighPart   << ", "
    << "LP: 0x"   << std::setw( 8) << li.LowPart    << ", "
    << "u.HP: 0x" << std::setw( 8) << li.u.HighPart << ", "
    << "u.LP: 0x" << std::setw( 8) << li.u.LowPart  << ", "
    << "QP: 0x"   << std::setw(16) << li.QuadPart   << "]"
    << std::dec << std::setfill(' ');
}

并将代码更改为:

bool test()
{
    LARGE_INTEGER start, end, freq;
    cout << "freq:" << endl;
    cout << freq << endl;
    if (!QueryPerformanceFrequency(&freq)) {
        cout << "QueryPerformanceFrequency failed!\n";
        return false;
    }
    cout << freq << endl;

    cout << "start:" << endl;
    cout << start << endl;
    QueryPerformanceCounter(&start);
    cout << start << endl;

    Sleep(1000);  // Simulate work

    cout << "end:" << endl;
    cout << end << endl;
    QueryPerformanceCounter(&end);
    cout << end << endl;

    cout << "range: from " << start.QuadPart << " to " << end.QuadPart << endl;
    return true;
}

产生以下输出:

freq:
[HP: 0xcccccccc, LP: 0xcccccccc, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
[HP: 0x00000000, LP: 0x0025a801, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
start:
[HP: 0xcccccccc, LP: 0xcccccccc, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
[HP: 0x0000000a, LP: 0xa6b8ff15, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
end:
[HP: 0xcccccccc, LP: 0xcccccccc, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
[HP: 0x0000000a, LP: 0xa6dfb945, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
range: from -3689348814741910324 to -3689348814741910324

因此,神秘值 -3689348814741910324 只不过是 QuadPart 的默认未初始化值:0xcccccccccccccccc。所以调用 QueryPerformanceCounter(&start);没有更新 QuadPart,而是更新了 LowPartHighPart。从中可以明显看出如何将 QueryPerformanceCounter 的实际返回值提取为 LONGLONG:

// Extract the HighPart/LowPart pair from a LARGE_INTEGER as a LONGLONG.
LONGLONG odd_extract(LARGE_INTEGER li) {
    return (static_cast<LONGLONG>(li.HighPart) << 32) + li.LowPart;
}

现在将测试中的最后一个输出替换为:

cout << "range: from " << odd_extract(start) << " to " << odd_extract(end) << endl;

输出

range: from 47158533369 to 47161073403

最后,以秒为单位计算耗时会返回一个期望值:

LONGLONG elapsed = odd_extract(end) - odd_extract(start);
double seconds = static_cast<double>(elapsed) / odd_extract(freq);
cout << "elapsed: " << seconds << " s" << endl;

输出

elapsed: 1.02861 s

这是 Windows 不准确的 Sleep() 所预期的。

现在我的问题是:QueryPeformanceCounter 不应该在提供的 LONG_INTEGERQuadPart 字段中返回其值吗?而不是 HighPart/LowPart?我在任何地方都找不到这是系统特定的,但似乎是这样。

更新 1

系统:64 位 Windows 7 企业版

编译器/IDE:MVS 2010 v. 10.0.40219.1 SP1Rel

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\WinNT.h中_LARGE_INTEGER的定义:

typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG HighPart;
    } DUMMYSTRUCTNAME;
    struct {
        DWORD LowPart;
        LONG HighPart;
    } u;
#endif //MIDL_PASS
    LONGLONG QuadPart;
} LARGE_INTEGER;

不过,尽管 LARGE_INTEGER 是一个 union ,但它的行为似乎并不像一个......

更新 2

我似乎没有在新的解决方案/项目中看到这种行为。也许解决方案中存在另一个问题,我正在尝试衡量导致此问题的性能。

无论如何,我仍然不知道为什么会发生这种情况,所以欢迎任何关于如何解决它的建议,谢谢!

最佳答案

有一个

#define union   struct

在我正在调试的项目的头文件中。

我哭了。

关于c++ - union 对象就像一个结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30262509/

相关文章:

c++ - 使用 `createProcess()` 调用 Webots 时传递参数

windows - FOR/F 循环中的虚假 'system\cmd.exe'

performance - 在 postgresql 的列上设置 "NOT NULL"会提高性能吗?

r - dplyr : row_number() from tbl_dt inconsistent to tbl_df 中的唯一行

c++ - 派生类映射

c++ - 在 pcm 上降低音量后出现奇怪的噪音

c++ - 编译c/cpp文件时scons "Depends"什么时候有用?

windows - 用Linux和Mac代码完成这3种方法,Memory Info平台独立类

mysql - 使用 int 和 char(32) 的 PK 性能

c++ - 如何限制缓存内存ubuntu?