为界限在C或C ++检查贵吗? [英] Is bounds checking in C or C++ expensive?

查看:99
本文介绍了为界限在C或C ++检查贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

边界检查价格昂贵
  (> X2次运行时开销)

Bound checking is expensive (> x2 times runtime overhead)

我从我的一个教授有上面这点。我感到困惑的。
据我所知,程序最耗时的部分是IO(从网络和硬盘)。

I got this point above from one of my professors. I am confused about that. As I know, the most time-consuming part of a program is IO(from network and from hard disks).

但在边界C或C ++检查并不总是与2输入源有关。
例如,我复制一个BUFF到另一个使用C中的的memcpy(DEST,SRC,长度(SRC))含量。在此之前,我检查的src 的大小,以prevent从一个堆溢出。在precess我可以想像的是:获取的src 的起始地址和 \\ X00 字节的src (汇编语言的角度来看,我复制的src 逐一看看是否字节相当于与 \\ X00 )。获得2地址后,只是他们。减去获得的的src 的长度。我读的src 从内存中的内容。我们都知道,从内存中读取的东西快。

But bounds checking in C or C++ are not always related with those 2 input sources. For example, I copy the content of one buff to another in C using memcpy(dest, src, length(src)). Before that, I check the size of src in order to prevent from a heap overflow. The precess I can image is: Get the start-address of src and the \x00 byte in src(in the view of assembly language, I copy the content of src one by one and see if the byte is equivalent with \x00). After getting the 2 address, just substract them to get the length of src. I read the content of src from the memory. We all know reading things from memory is fast.

推荐答案

我刚跑了一个程序,我与迭代边界检查开启。

I just ran a program I had with iterator bounds checking turned on.

运行时间从789毫秒到2608毫秒。

The running time went from 789 ms to 2608 ms.

所以,是的,它能够决定的事情。不是所有的时间,但肯定比从不多。

So yes, it can matter. Not all the time, but certainly more than never.

在特别是,结合的核对迭代需要至少两倍的存储一样简单的指针,而且,不容易地优化。从理论上讲,他们是简单而有效的,但实际上,你根本就不想这样做,你不需要工作。

In particular, bound-checked iterators require at least twice as much storage as simple pointers, and furthermore, are not as easily optimized. In theory they're simple and efficient, but in practice you simply don't want to do work that you don't need to.

哦,我提到了的编译的时间从7.72秒去13.21秒呢?

Oh, and did I mention the compile time went from 7.72 seconds to 13.21 seconds as well?

有关你在众多的非信徒......一个微型的例子接受0.92秒没有边界检查并的1.96秒

For the many nonbelievers among you... a miniature example takes 0.92 seconds without bounds checking and 1.96 seconds with.

由于有一个约的一切很多的怀疑的,包括矢量的工作效率......这里的另一个问题:

Since there's a lot of skepticism about everything, including vector's efficiency... here's another one:

#include <cstdio>
#include <ctime>

template<class T> struct Vector
{
    T *b, *e;
    Vector(size_t n) : b(new T[n]), e(b + n) { }
    T &operator[](size_t i) { return b[i]; }
    T &at(size_t i) { if (i >= e - b) { throw "invalid"; } return b[i]; }
};

#define at operator[]  // Comment this out to enable bounds-checking

int main(int argc, char **argv)
{
    Vector<size_t> v(1 << 16);
    for (size_t *p = v.b; p != v.e; ++p) { *p = 1; }
    clock_t begin = clock();
    for (int j = 0; j < 1 << 12; ++j)
    {
        for (size_t i = 8, n = v.e - v.b; i < n; ++i)
        {
            v.at(i) += v.at(i - 8);
            v.at(i) ^= v.at(i - 7);
            v.at(i) -= v.at(i - 6);
            v.at(i) ^= v.at(i - 5);
            v.at(i) += v.at(i - 4);
            v.at(i) ^= v.at(i - 3);
            v.at(i) -= v.at(i - 2);
            v.at(i) ^= v.at(i - 1);
        }
    }
    clock_t end = clock();
    fprintf(stderr, "%u\n", clock() - begin);
}

这篇关于为界限在C或C ++检查贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆