Speed of memset Vs direct assignment to zero

Viewed 1665

Please consider a struct definition below.

struct xyz {
  char a;
  void *ptr;
  uint16_t num1;
  uint32_t num2;
  uint64_t num3;
};

I can initialize an instance of this structure to zero in one of 3 ways as below.

  1. At time of declaration

    struct xyz instance = { 0 };
    
  2. Sometime after declaring it using the C-99 feature.

    instance = (const struct xyz) { 0 };
    

3.memset (&instance, 0, sizeof (struct xyz));

A typical C struct could have some padding added by the compiler for alignment purpose. So memset will initialize the padding bytes also to zero.

My question is:

In general, will initialization by method 1 or 2 above be faster than method 3? It doesn't matter how many CPU cycles are saved, I am just curious if 1 & 2 are faster than 3 or not.

1 Answers
Related