cphb
cphb copied to clipboard
Memset for distance arrays
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?
That's a good idea, I'll consider it. In any case, the book should mention the memset function.
GCC will generally convert a zero-initialization loop to a memset
, so there's no efficiency difference.
Only with -O3, and most judges (for instance codeforces, uva, POI) use -O2, so there's usually an efficiency difference.
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.