0

With structure :

struct element_data
{
    int size;
    int type;
    int nID;
    int attribute;
    double fValue;
    int mode;
    bool bTemp;
};

...I have to manually initialize all members with zero :

inline element_data() : //Empty constructor
    size(0),
    type(0),
    nID(0),
    attribute(0),
    fValue(0),
    mode(0),
    bTemp(false)
{}

Too many commands, that may slowdown my calculator. Is there any other way which can do this? For example with a single command all values will take the zero values...

  • Are you **absolutely certain** that this is your performance bottleneck? Trying to avoid language features that solve your problem in the interest of performance is almost usually a Very Bad Idea unless you have documented proof that it's really the problem. – templatetypedef Feb 07 '13 at 01:53
  • you could fake it. but the number of source-code commands wont affect the speed of this. This is as fast as it gets. – Sellorio Feb 07 '13 at 01:53
  • 3
    Before worrying about how to do this differently, you should measure whether it really slows anything down. – juanchopanza Feb 07 '13 at 01:53
  • 2
    `element_data object = {0};` – imreal Feb 07 '13 at 01:54
  • Initializer list is meant to be a optimized version of `a=b` `c=d` etc. It may not be as bad as you think.. – Karthik T Feb 07 '13 at 02:01
  • Alternatively, take a look at [Do the parentheses after the type name make a difference with new?](http://stackoverflow.com/a/620402), might help with readability if nothing else. – Karthik T Feb 07 '13 at 02:02
  • Well, *do* test this. And find another compiler if you see any difference. – Hans Passant Feb 07 '13 at 02:03

2 Answers2

1

No. There isn't. At least, if efficiency is your concern.

Here's why

I quote, from the above link, "All other things being equal, your code will run faster if you use initialization lists rather than assignment."

Remember, just because it's a lot of code, doesn't mean its less efficient. In fact, optimized code usually tends to look larger and more complex than unoptimized, general purpose code. This is usually because you're taking short cuts based on the properties of the data you're processing or on the algorithm you're using.

The advantage with this approach is that it does not rely on the code that creates the instance to correctly initialize the members to zero, while being efficient too. An initializer list makes sure your structure members are always initialized correctly.

Carl
  • 43,122
  • 10
  • 80
  • 104
0

As your struct element_data is POD you could initialize members to default value in two ways:

element_data ed = element_data();

OR

element_data ed = {};

All integer members will be initialized to 0 and bool will be initialized to false.

billz
  • 44,644
  • 9
  • 83
  • 100
  • The second example throws a compiling error, and the first definition only works right if I manually initialize it. –  Feb 07 '13 at 02:19
  • What compiler do you have? btw, you don't need a default constructor for first one. see this: http://ideone.com/OHMXfp – billz Feb 07 '13 at 02:20
  • I have Visual C++, and `element_data ed = {0};` - it's correct. –  Feb 07 '13 at 02:23
  • well, you don't need `0` inside `{}`, coz that initialize first member to 0 and the rest to default value – billz Feb 07 '13 at 02:24
  • All values are "**zero**". I'm happy now. :) –  Feb 07 '13 at 02:26
  • I discovered `element_data ed = {0};` is certainly faster. Did 500.000.000 loops it only takes **9.25** sec while with initializer list it takes up to **13.047** seconds!!!! –  Feb 07 '13 at 03:10
  • compare to which one? you know {0} is the same as {} ? – billz Feb 07 '13 at 03:13