cphb icon indicating copy to clipboard operation
cphb copied to clipboard

Memset for distance arrays

Open L1nkus opened this issue 7 years ago • 4 comments

Calling memset is more efficient and quicker to write, than a loop, to set all values to 0/INF.

What do you think about changing the loops in graph algorithms to memset?

L1nkus avatar May 01 '17 11:05 L1nkus

That's a good idea, I'll consider it. In any case, the book should mention the memset function.

pllk avatar May 02 '17 11:05 pllk

GCC will generally convert a zero-initialization loop to a memset, so there's no efficiency difference.

jaens avatar Sep 16 '17 17:09 jaens

Only with -O3, and most judges (for instance codeforces, uva, POI) use -O2, so there's usually an efficiency difference.

L1nkus avatar Sep 17 '17 08:09 L1nkus

You can just put a #pragma GCC optimize ("O3") in your code instead of hand-optimizing it when you do not like the default optimization flags.

jaens avatar Sep 17 '17 19:09 jaens