memset time complexity

Can anyone tell me what is the time complexity of memset to initialise an array of size N.

1 Like
1 Like

Complexity of memset is O(n).

tnx @betlista ,@sobhagya …btw i was thinking ,is there any function which resets all elements of a vector in constant time

@andreyv: This might look interesting to you c++ - How to zero out array in O(1)? - Stack Overflow

1 Like

This function has to reset each element and there are n elements – how can it be faster than O(n)?

Every optimization is a tradeoff. It is lazy clearing and while normal array usage is O(n+m) where m is number accesses to array, here it is O(2m) and it depends on relation between the two - if n >> m lazy is better, for n ~= m it is the same and for n << m the first is better…

1 Like

@betlista >> Agreed. I thought better share it, because neither the intention of the question is clear nor the domain where he is going to apply.