计算一个std::vector的复制和移动次数
Counting the number of copy and move of a std::vector
我有一个用 C++11 编写的程序,我想计算 std::vector<double>
对象的移动和复制(构造和赋值)次数。有办法吗?
此致
否。 std::vector<>
的实现没有为此提供任何方法。我知道的任何编译器也没有。
如评论中所述,您可以创建自己的计数替代品并改用它。即
struct counting_vector_double
: std::vector<double>
{
static size_t count_copy_ctor;
static size_t count_move_ctor;
counting_vector_double(counting_vector_double const&other)
: std::vector<double>(other)
{ count_copy_ctor++; }
// etc
};
// in .cc file:
size_t counting_vector_double::count_copy_ctor = 0;
size_t counting_vector_double::count_move_ctor = 0;
(在 multi-threaded 情况下使用 atomic
计数器。)要实现这一点,您可以使用 typedef
或 using
指令,例如
#ifdef CountingMoveCopy
using vector_double = counting_vector_double;
#else
using vector_double = std::vector<double>;
#endif
并在代码中使用 vector_double
。
一种可能的规避限制的方法是使 class Vector 继承 std::vector,然后重载移动和复制运算符以增加内部计数器。
这个答案考虑到 OP 需要计算向量项被复制、移动、构造等的次数,而不是向量本身。
虽然这不是您问题的直接答案,但您可能对这个答案感兴趣。
围绕双倍做一个小包装 class:
struct double_wrapper{
double_wrapper()=delete;
double_wrapper(const double value):value_(value){
++constructions_count;
}
double_wrapper(const double_wrapper& other){
value_=other.value_;
++copies_count;
}
double_wrapper(double_wrapper&& other){
value_=other.value_;
//invalidate it in someway.. maybe set it to 0 (not important anyway)
++moves_count;
}
operator double() const {
return value_;
}
double value_;
static unsigned int copies_count;
static unsigned int constructions_count;
static unsigned int moves_count;
}
// in .cpp
unsigned int double_wrapper::copies_count=0;
unsigned int double_wrapper::constructions_count=0;
unsigned int double_wrapper::moves_count=0;
最后,您必须编辑 vector
类型(您可以使用一些 #ifdef
将其缩短为调试模式):
std::vector<double_wrapper> v1;
注:未测试。
我有一个用 C++11 编写的程序,我想计算 std::vector<double>
对象的移动和复制(构造和赋值)次数。有办法吗?
此致
否。 std::vector<>
的实现没有为此提供任何方法。我知道的任何编译器也没有。
如评论中所述,您可以创建自己的计数替代品并改用它。即
struct counting_vector_double
: std::vector<double>
{
static size_t count_copy_ctor;
static size_t count_move_ctor;
counting_vector_double(counting_vector_double const&other)
: std::vector<double>(other)
{ count_copy_ctor++; }
// etc
};
// in .cc file:
size_t counting_vector_double::count_copy_ctor = 0;
size_t counting_vector_double::count_move_ctor = 0;
(在 multi-threaded 情况下使用 atomic
计数器。)要实现这一点,您可以使用 typedef
或 using
指令,例如
#ifdef CountingMoveCopy
using vector_double = counting_vector_double;
#else
using vector_double = std::vector<double>;
#endif
并在代码中使用 vector_double
。
一种可能的规避限制的方法是使 class Vector 继承 std::vector,然后重载移动和复制运算符以增加内部计数器。
这个答案考虑到 OP 需要计算向量项被复制、移动、构造等的次数,而不是向量本身。
虽然这不是您问题的直接答案,但您可能对这个答案感兴趣。
围绕双倍做一个小包装 class:
struct double_wrapper{
double_wrapper()=delete;
double_wrapper(const double value):value_(value){
++constructions_count;
}
double_wrapper(const double_wrapper& other){
value_=other.value_;
++copies_count;
}
double_wrapper(double_wrapper&& other){
value_=other.value_;
//invalidate it in someway.. maybe set it to 0 (not important anyway)
++moves_count;
}
operator double() const {
return value_;
}
double value_;
static unsigned int copies_count;
static unsigned int constructions_count;
static unsigned int moves_count;
}
// in .cpp
unsigned int double_wrapper::copies_count=0;
unsigned int double_wrapper::constructions_count=0;
unsigned int double_wrapper::moves_count=0;
最后,您必须编辑 vector
类型(您可以使用一些 #ifdef
将其缩短为调试模式):
std::vector<double_wrapper> v1;
注:未测试。