相关疑难解决方法(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万
查看次数

如果REST API返回JSON,那么MIME类型是什么?

我的REST API返回JSON.

我目前正在返回text/plain作为MIME类型,但感觉很有趣.我应该回来application/x-javascript还是其他类型?

第二个问题是关于错误条件的HTTP状态代码.如果我的REST API返回错误状态,我将返回JSON

{ result: "fail", errorcode: 1024, errormesg: "That sucked. Try again!" }
Run Code Online (Sandbox Code Playgroud)

HTTP状态代码应该保留在200 OK

api rest json http mime-types

67
推荐指数
5
解决办法
5万
查看次数

标签 统计

api ×1

branch-prediction ×1

c++ ×1

http ×1

java ×1

json ×1

mime-types ×1

optimization ×1

performance ×1

rest ×1