小编Exi*_*xiD的帖子

谁更快:BOOST_FOREACH宏或for

我测试了我的程序,并决定将BOOST_FOREACH宏更改为简单for循环const_iterator.

我得到意想不到的结果:程序工作较慢for.

然后我写了小测试应用程序:

std::vector<int> vec;
for (int i = 0; i != 50000000; ++i)
    vec.push_back(i);



time_t t1 = clock();
int sum1 = 0;
for (std::vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it)
    sum1 += *it;
std::cout << ulong(clock() - t1);

time_t t2 = clock();
int sum2 = 0;
BOOST_FOREACH(auto &it, vec) {
    sum2 += it;
}
std::cout << ulong(clock() - t2);
Run Code Online (Sandbox Code Playgroud)

这是输出:

34963
26964
Run Code Online (Sandbox Code Playgroud)

为什么这样?

c++ macros boost const-iterator

0
推荐指数
1
解决办法
58
查看次数

内存映射文件C++

请帮我阅读内存映射文件.我在下面的代码中打开文件.然后我想从8到16读取字节.我该怎么做?

// 0. Handle or create and handle file
m_hFile = CreateFile(file_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
    {
        m_hFile = createNewFile(file_path.c_str());
    }
    else throw GetLastError();
}

// 1. Create a file mapping object for the file
m_hMapFile = CreateFileMapping(m_hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (m_hMapFile == NULL) throw GetLastError();

// 2. Map the view.
m_lpMapAddress = MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
// to map
if (m_lpMapAddress == …
Run Code Online (Sandbox Code Playgroud)

c++ winapi file

0
推荐指数
1
解决办法
1612
查看次数

标签 统计

c++ ×2

boost ×1

const-iterator ×1

file ×1

macros ×1

winapi ×1