类模板中的性能
performance in class template
本文关键字:性能 更新时间:2023-10-16
我的类模板如下
template<class T>
class A
{
public:
A(T t) : m_t(move(t)) {}
operator T const&() const { return m_t; }
private:
T m_t;
};
当T
是int
(1)构造函数中的move
是否具有性能下降而没有移动?
(2)operator T const&()
是否具有与operator T&()
相比的性能下降?
- 复制和移动和int是相同的
考虑:
struct A {
int A;
};
struct B {
std::string str;
};
对于a,复制和移动是相同的(复制嵌入的int)。对于B,复制是为字符串分配内存(通常)。但是,移动将只是复制指向从字符串,O(1)操作以及可能的大小的指向堆分配的内存的指针。字符串具有远程零件,因此,仅移动指针时。
- 编译器非常擅长优化,我相信它将为INT生成相同的代码,因为INT不是多态或可衍生的(类)。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- OpenMP阵列性能较差
- 递归列出所有目录中的C++与Python与Ruby的性能
- 大小相等但成员数量不同的结构之间的性能差异
- 为什么constexpr的性能比正常表达式差
- 在类中使用随机生成器时出现性能问题
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 海湾合作委员会 ARM 性能下降
- GCC 和 Clang 代码性能的巨大差异
- 在容量内调整矢量大小时的性能影响
- 了解算法的性能差异(如果以不同的编程语言实现)
- 未达到的情况会影响开关外壳性能
- QStringList vs list<shared_ptr<QString>> 性能比较C++
- 是否总是可以将使用递归编写的程序重写为不使用递归的程序C++,性能观点是什么?
- 哪种方法更好,性能明智
- C++ 特征库:引用的性能开销<>
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- 基于范围的 for 循环range_declaration中各种说明符之间的性能差异
- std::p mr::memory_resource 如何与 std::container 产生性能差异?
- 不同的类或结构初始化方法之间的性能差异是什么?