什么是零现有阵列的最快方法? [英] What is the fastest way to zero an existing array?

查看:107
本文介绍了什么是零现有阵列的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的一维数组,是 memset的以最快的方式零呢?

I have an existing 1D array, is memset the fastest way to zero it?

推荐答案

最快...或许是肯定的。
越野车几乎可以肯定!

Fastest ... probably yes. Buggy almost sure!

这主要取决于实施平台上...数组包含什么类型的。

It mostly depends on the implementation, platform and ... what type the array contains.

在C ++中,当变量定义它的构造函数被调用。当定义一个阵列,所有的数组元素的构造函数的调用。

In C++ when a variable is defined its constructor is called. When an array is defined, all the array's elements' constructors are called.

可以当数组类型知道有没有了可以再被所有零,其默认构造函数不执行任何psented $ P $初始状态被认为只用于案件好采取行动。

Wiping out the memory can be considered "good" only for the cases when the array type is know to have an initial state that can be represented by all zero and for which the default constructor doesn't perform any action.

这是一般的如此内建类型,但也是假的其他类型的。

This is in general true for built-in types, but also false for other types.

最安全的方式是分配一个默认的元素初始化暂时的。

The safest way is to assign the elements with a default initialized temporary.

template<class T, size_t N>
void reset(T* v)
{
    for(size_t i=0; i<N; ++i) 
        v[i] = T();
}

需要注意的是,如果T 字符,该函数实例化,准确地翻译为 memset的。因此,它是相同的速度,不多不少。

Note that, if T is char, the function instantiates and translates exactly as memset. So it is the same speed, no more no less.

这篇关于什么是零现有阵列的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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