哪个更好:保留向量容量,预先分配大小或循环回退? [英] What is better: reserve vector capacity, preallocate to size or push back in loop?

查看:58
本文介绍了哪个更好:保留向量容量,预先分配大小或循环回退?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数将指向char数组和段大小的指针作为输入参数,并调用另一个需要 std :: array< std :: string> 的函数。 。想法是将输入的char数组切成相等的部分,并形成字符串数组。

I have a function that takes a pointer to char array and segment size as input arguments and calls another function that requires a std::array<std::string>. The idea is that the input char array is "sectioned" into equal parts, and string array formed.

输入的char数组格式是以下几个较小的数组(或字符串)确定大小,并置在一起。尽管它们可能是零,但并不假定它们是零终止的。段大小5和元素数量10的示例:

The input char array format is several smaller arrays (or strings) of determined size, concatenated togeather. These are not assumed zero-terminated, although they might be. Examples for segment size 5 and number of elements 10:

char k[] = "1234\0001234\0001234\0001234\0001234\0001234\0001234\0001234\0001234\0001234\000";
char m[] = "1234\00067890987654321\000234567809876\0005432\000\000\0003456789098";
char n[] = "12345678909876543211234567890987654321123456789098";

所有char数组的长度为51(段*元素+ 1)。我的目标是使函数有效地利用资源,最重要的是执行时间。

Length of all char arrays is 51 (segment * elements + 1). My goal is to make the function use resources efficiently, most importantly execution time.

由于有很多方法可以给猫换皮,所以我有两种(或三种)方法解决这个问题,问题是,哪个更好?我的意思是说更快,更少浪费资源。我不是专业人士,所以请耐心等待。

Since there are many ways to skin a cat, I have two (or three) ways to tackle this, and the question is, which is "better"? By that I mean faster and less resource-wasteful. I am not a professional, so be patient with me.

在这里,预先分配了个值,然后分配了每个字符串

Here, values is preallocated and then each string assigned a value.

void myfnc_1(void *a_src, uint32_t a_segment) {
    // a_segment = 5 for example
    size_t nSize = GetSize(); // another method, gets 10
    std::vector<std::string> values(nSize);
    char* v = a_src; // take k, n or m for example

    for (size_t i = 0; i < nSize; ++i) {
        values.at(i).assign(v, a_segment);
        v += a_segment;
    }
}

此处,未分配向量,但每次迭代

Here, the vector is not allocated, but each iteration a new string is added.

void myfnc_1(void *a_src, uint32_t a_segment) {
    size_t nSize = GetSize();
    std::vector<std::string> values();
    char* v = a_src;

    for (size_t i = 0; i < nSize; ++i) {
        values.push_back("");
        values.back().assign(v, a_segment);
        v += a_segment;
    }
}

可能还有第三种方法,那就更好。我对向量的经验不多,所以我不完全了解。如果段长度和元素数通常较大(5、10)或较小(100、10000),会有所不同吗?

There might be a third way, that's better. I'm not so experienced with vectors so I don't know exactly. Do the segment length and number of elements make a difference if they are usually large (5, 10) or small (100, 10000)?

第一篇文章,大粉丝: )

First post, big fan :)

推荐答案

在避免动态重新分配时将达到更好的性能,因此,请尝试使向量内存足够大以容纳所有元素

Better performance will be reached when avoiding dynamic reallocation, so try to have the vector memory be big enough to receive all elements.

您的第一个解决方案将更加高效,因为如果nSize大于默认矢量容量,则第二个解决方案将需要重新分配以存储所有元素。

Your first solution will be more efficient because if nSize is bigger than default vector capacity, the second one will need a reallocation to be able to store all elements.

如Melkon所说,储备更好:

As commented by Melkon, reserve is even better:

void myfnc_1(void *a_src, uint32_t a_segment) {
    size_t nSize = GetSize();
    std::vector<std::string> values;
    values.reserve( nSize );
    char* v = a_src;

    for (size_t i = 0; i < nSize; ++i) {
        values.push_back( std::string( v, a_segment ) );
        v += a_segment;
    }
}

这篇关于哪个更好:保留向量容量,预先分配大小或循环回退?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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