calloc v/s malloc and time efficiency

前端 未结 7 1221
抹茶落季
抹茶落季 2021-02-04 08:12

I\'ve read with interest the post C difference between malloc and calloc. I\'m using malloc in my code and would like to know what difference I\'ll have using calloc instead.

7条回答
  •  野的像风
    2021-02-04 08:47

    Your point stated in 3. seems to indicate a case or unnecessary initialization. That is pretty bad speed wise, not only the time spent doing it is wasted but a whole lot of cache eviction happened because of it.

    Doing a memset() or bzero() (that are called by calloc() anyway) is a good way to invalidate huge portion of your cache. Don't do it unless you are sure you won't overwrite everything yet can read parts of the buffer that will not have been written (as if 0 is an acceptable default value). If you write over everything anyway by all mean don't initialize your memory unnecessarily.

    Unnecessary memory writing will not only ruin your app performance but also the performance of all applications sharing the same CPU with it.

提交回复
热议问题