简体   繁体   中英

The most efficient way to assign values to a bunch of variables

Suppose we have some integer variables, and all of them should get their values from a struct called "numberpool" via function call, and the value assignment should only be done by "entrykey":

struct numberpool{
    vector<int> numbers(100);
};

void assign(struct numberpool& src, int entrykey, int& dest){  // call by reference
    dest = src.numbers[tag];
    return ;
}

int main(){
    struct numberpool pool;  
    ...... // pool initialization

    int a, b, c, d, e .... z;   // 26 integers declaration

    // Assigning
    assign(pool, 1, a);
    assign(pool, 5, b);
    assign(pool, 23, c);

    ....and so on.

    return 0;
}

I'd like to know, is this the fastest way to complete this job? I doubt that there is more efficient way to do this, due to the frequently function calls.
What if I define another struct containing all the int variables, and just call a function once, would it help ?

The performance of the function calls is not your problem here. The compiler will be able to optimize it quite well.

However, seeing so many variables makes me cringe. Unless you really have a meaning behind each and every of the variables (apart from being letters of the alphabet), you should think about another design. If you really want to map the alphabet to numbers, you should just use an array.

Use method instead of standalone function and assign natural way:

struct numberpool{
    vector<int> numbers(100);
    int get( size_t tag ) const { return numbers[tag] };
};

int main(){
    numberpool pool;  
    ...... // pool initialization

    int a = pool.get(1);
    int b = pool.get(5);

    ....and so on.

    return 0;
}

If the majority of your "tag" indices are grouped closely together, you can copy the underlying data as a single block:

struct subrange{
    static size_t min_ndx() constexpr { return 1; }
    static size_t max_ndx() constexpr { return 23; }
    static size_t storage_size() constexpr { return max_ndx() - min_ndx() + 1; }

    array<int, storage_size()> storage;
    int outlier_99; // Avoid copying indices between 24 and 99

    // accessors
    int &a() { return storage[1 - min_ndx()]; }
    int &b() { return storage[5 - min_ndx()]; }
    int &c() { return storage[23 - min_ndx()]; }
    int &d() { return outlier_99; }

    // Constructor allows numberpool to copy its vector
    subrange(const vector<int>& numbers)
        :storage(numbers.cbegin() + min_ndx(),
                 numbers.cbegin() + min_ndx() + storage_size()),
        outlier_99(numbers[99])
    {}
};

struct numberpool{
    vector<int> numbers(100);
    subrange copy_subrange( ) const {
        return subrange(numbers); };
    };

main()
{
    numberpool pool;  
    ...... // pool initialization

    // Copy a subset of memory, including data you're interested in,
    // and some chunks in between you're not.
    auto extracted = pool.copy_subrange();

    int sum = extracted.a() + extracted.b() + extracted.c() + extracted.d();

Copying a block of memory may or may not be faster, depending on your exact situation. When in doubt, try both ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM