初始化每个班级成员时的性能问题? [英] Performance issues when initializing each class member?
问题描述
我认为这个SO问题是关于初始化的一般观点,它已经回答了很多问题:
I consider this SO question as a general point of view regarding the initialization and it answers already a lot:
但是,当我的类有100个成员并且我用{}
命令初始化每个成员时,我并没有发现任何有关可能的性能问题的信息,仅仅是因为Eclipse警告我有关未初始化的成员的信息:
But, I didn't find really anything regarding a possible performance issue when for example my class has 100 members and I initialize each member with the {}
command, just because Eclipse is warning me about uninitialized member:
成员"foo"未在此构造函数中初始化
Member 'foo' was not initialized in this constructor
我的问题:每次实例化此类时,具有很多成员[> 50]的类中每个成员的初始化都会导致性能问题吗?
My question: Can an initialization of each member in a class with lot of members [>50] lead to a performance issue each time this class is instantiated?
更新:由于第一条评论:我总体上是在问. Eclipse在我的项目中警告我9次,分为4类!
Update: Due to the first comments: I am asking in general. Eclipse is warning me in my project 9 times split over 4 classes!
推荐答案
以下是我在gcc中运行它的结果.
我用不同的班级成员人数测试了它[5次并取平均值],但是实例化了班级100'000'000.
I tested it [5-times and took the average] with different count of class members but with 100'000'000 instantiation of the class.
这有点棘手,因为我使用了最高的优化级别-O3
,因此必须避免编译器优化代码.
It was a bit tricky because I used the highest optimization level -O3
and therefore I had to avoid the compiler optimizes code away.
有100个班级成员:
Initialized class members: 4'484 msec
Not initialized class members: 50 msec
有25个班级成员:
Initialized class members: 1'146 msec
Not initialized class members: 50 msec // as expected it didn't change
有500名班级成员:
Initialized class members: 22'129 msec
Not initialized class members: 50 msec // as expected it didn't change
我的结论是:
在初始化所有类成员时,在正常情况下会出现- - 否 明显的性能问题.在通常情况下,使用 的意思是,当有一个函数(带有其他代码)具有100'000'000次迭代时,成员初始化实际上不计在内.
There is - under normal circumstances - no remarkable performance issue when all class members are initialized. With under normal circumstances I mean that when there is a function [with other code] with 100'000'000 iterations that the member initialization really doesn't count.
如评论中所述,一个好的设计不应有那么多的班级成员-我只是很好奇.
As stated in the comments a good design shouldn't have so many class members - I was just general curious.
PS:
我检查了汇编程序列表,并且-当然-实际上,在初始化版本中,gcc用movq $0, 40(%rsp)
初始化了每个int
成员-40
是堆栈上的位置.
I checked the assembler listing and - of course - indeed in the initialized version gcc initialized each int
member with movq $0, 40(%rsp)
- the 40
is the position on the stack.
#include <stdlib.h>
#include <stdio.h>
#include <chrono>
#include <ctime>
#include <utility>
template<typename TimeT = std::chrono::microseconds>
class measure
{
public:
template<typename F, typename ...Args>
static typename TimeT::rep execution(F func, Args&&... args)
{
auto start = std::chrono::system_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::system_clock::now() - start);
return duration.count();
}
};
class foo
{
// for uninitialized version remove the {}
size_t mainValue {};
size_t classMember0 {};
...
size_t classMember499 {};
public:
foo( size_t value ) : mainValue (value + 4660) {};
auto getMainValue() -> size_t { return mainValue; };
};
auto runCode( size_t iterationCount ) -> void
{
size_t someValue {};
for ( size_t j = 0 ; j < iterationCount ; ++j )
{
foo MyFoo (iterationCount);
someValue += MyFoo.getMainValue();
}
printf( "Result=%ld\n", someValue ); // that the whole code isn't optimized away...
}
int main( int argc, char * argv [] )
{
if ( argc != 2 )
{
printf( "Usage: %s <Start-Value>\n", argv [0] );
return 0;
}
size_t variableValue = (size_t) atof( argv [1] );
auto threadExecutionTime = measure<>::execution( [&] () { runCode( variableValue ); } );
printf( "Total execution time was %.3f milliseconds. %s",
threadExecutionTime / 1000. );
}
这篇关于初始化每个班级成员时的性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!