相关疑难解决方法(0)

为什么处理排序数组比处理未排序数组更快?

这是一段看似非常特殊的C++代码.出于某种奇怪的原因,奇迹般地对数据进行排序使得代码几乎快了六倍.

#include <algorithm>
#include <ctime>
#include <iostream>

int main()
{
    // Generate data
    const unsigned arraySize = 32768;
    int data[arraySize];

    for (unsigned c = 0; c < arraySize; ++c)
        data[c] = std::rand() % 256;

    // !!! With this, the next loop runs faster.
    std::sort(data, data + arraySize);

    // Test
    clock_t start = clock();
    long long sum = 0;

    for (unsigned i = 0; i < 100000; ++i)
    {
        // Primary loop
        for (unsigned c = 0; c < arraySize; ++c) …
Run Code Online (Sandbox Code Playgroud)

c++ java optimization performance branch-prediction

2万
推荐指数
27
解决办法
142万
查看次数

在哪种情况下我使用特定的STL容器?

我一直在阅读关于C++的书中的STL容器,特别是关于STL及其容器的部分.现在我明白了每一个都有自己的特定属性,而且我已经接近记住了所有这些...但我还没有掌握的是在哪种情况下使用它们.

解释是什么?示例代码是更受欢迎的.

c++ stl container-data-type

179
推荐指数
6
解决办法
6万
查看次数