避免 C++ 中的小页面错误使用 g++ 进行编程

发布于 2024-12-12 19:58:55 字数 1392 浏览 4 评论 0原文

我正在尝试解决这个难题:运输编码难题。这是我到目前为止提出的代码:

#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <set>
using namespace std;
struct Container
{
    Container(int c, int w) : cost(c), weight(w){}
    int cost;
    int weight;
    bool operator<(const Container& c) const
    {
        return double(cost)/weight < double(c.cost)/c.weight;
    }
};
int main(int argc, char** argv)
{
    int fd = open(argv[1], O_RDONLY);
    char* pContent = (char*)mmap(NULL, 128 * sizeof(int), PROT_READ, MAP_PRIVATE, fd, 0);
    pContent += 8;
    stringstream ss(pContent);
    int unload = 0;
    ss >> unload;
    set<Container> containers;
    int cost = 0, weight = 0;
    while(ss >> cost)
    {
        ss >> weight;
        containers.insert(Container(cost, weight));
    }

    const Container& best = *containers.begin();
    cost = best.cost * ceil(double(unload)/best.weight);
    printf("%d\n", cost);
    return 0;
}

我知道这个逻辑中可能存在一些错误。但我的问题与逻辑无关。当我提交此代码时,它成功运行,但我得到的分数较低,因为它显示次要页面错误的数量为 409。当我看到排行榜时,有人提交了带有轻微页面错误 69 的 C++ 代码。有什么办法可以控制这些轻微的页面错误吗?可能正在使用一些 g++ 标志?现在我的 make 文件非常简单:g++ -o MyExe MyExe.cc

I am trying to solve this puzzle: Shipping Coding Puzzle. This is the code I have come up so far:

#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <set>
using namespace std;
struct Container
{
    Container(int c, int w) : cost(c), weight(w){}
    int cost;
    int weight;
    bool operator<(const Container& c) const
    {
        return double(cost)/weight < double(c.cost)/c.weight;
    }
};
int main(int argc, char** argv)
{
    int fd = open(argv[1], O_RDONLY);
    char* pContent = (char*)mmap(NULL, 128 * sizeof(int), PROT_READ, MAP_PRIVATE, fd, 0);
    pContent += 8;
    stringstream ss(pContent);
    int unload = 0;
    ss >> unload;
    set<Container> containers;
    int cost = 0, weight = 0;
    while(ss >> cost)
    {
        ss >> weight;
        containers.insert(Container(cost, weight));
    }

    const Container& best = *containers.begin();
    cost = best.cost * ceil(double(unload)/best.weight);
    printf("%d\n", cost);
    return 0;
}

I know there may be some bugs in this logic. But my question is not related to the logic. When I submit this code it successfully runs but the score I get is less as it says the number minor page faults is 409. When I see the leader board somebody has submitted a C++ code with minor page faults 69. Is there any way I can control these minor page faults? may be using some g++ flags ? Right now my make file is very simple: g++ -o MyExe MyExe.cc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-12-19 19:58:55

你会受到 Gild 用于判断这些的算法和运行时环境的支配。我认为编译器标志可能是一个转移注意力的东西;我怀疑以“发布模式”提交会为您打开 -O2 或 -O3。

我怀疑这是一个优化内存使用的问题,可能是内存局部性的问题。例如,字符串流和集合可能会变得非常昂贵;站在你的立场上,我会开始寻找是否有另一种方法,也许采用更量身定制的算法。

You're at the mercy of whatever algorithm and runtime environment Gild use for judging these. I think that compiler flags are probably a red herring; I suspect that submitting in "release mode" turns on -O2 or -O3 for you.

I suspect it's a question of optimising for memory use, possibly memory locality. That stringstream and set could get very expensive, for example; in your shoes I'd start looking to see whether there was another way, perhaps with a more tailored algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文