为一堆变量赋值的最有效方法
The most efficient way to assign values to a bunch of variables
假设我们有一些整数变量,它们都应该通过函数调用从一个名为 "numberpool" 的结构中获取它们的值,并且赋值应该只由 "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;
}
我想知道,这是完成这项工作的最快方法吗?由于函数调用频繁,我怀疑是否有更有效的方法来执行此操作。
如果我定义另一个包含所有 int 变量的结构,并且只调用一次函数,会有帮助吗?
这里函数调用的性能不是你的问题。编译器将能够很好地优化它。
然而,看到这么多变数让我感到畏缩。除非你真的有每个变量背后的含义(除了是字母表中的字母),否则你应该考虑另一种设计。如果你真的想将字母映射到数字,你应该只使用一个数组。
使用方法而不是独立函数并分配自然方式:
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;
}
如果您的大多数 "tag" 索引紧密组合在一起,您可以将基础数据复制为一个块:
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();
复制内存块可能会或可能不会更快,具体取决于您的具体情况。如有疑问,请尝试两种方法。
假设我们有一些整数变量,它们都应该通过函数调用从一个名为 "numberpool" 的结构中获取它们的值,并且赋值应该只由 "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;
}
我想知道,这是完成这项工作的最快方法吗?由于函数调用频繁,我怀疑是否有更有效的方法来执行此操作。
如果我定义另一个包含所有 int 变量的结构,并且只调用一次函数,会有帮助吗?
这里函数调用的性能不是你的问题。编译器将能够很好地优化它。
然而,看到这么多变数让我感到畏缩。除非你真的有每个变量背后的含义(除了是字母表中的字母),否则你应该考虑另一种设计。如果你真的想将字母映射到数字,你应该只使用一个数组。
使用方法而不是独立函数并分配自然方式:
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;
}
如果您的大多数 "tag" 索引紧密组合在一起,您可以将基础数据复制为一个块:
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();
复制内存块可能会或可能不会更快,具体取决于您的具体情况。如有疑问,请尝试两种方法。