is memset() more efficient than for loop in C?

前端 未结 7 1392
不思量自难忘°
不思量自难忘° 2021-02-02 06:46

is memset more efficient than for loop. so if i have

char x[500];
memset(x,0,sizeof(x));

or

char x[500];
for(int i = 0 ; i &         


        
7条回答
  •  后悔当初
    2021-02-02 06:57

    It really depends on the compiler and library. For older compilers or simple compilers, memset may be implemented in a library and would not perform better than a custom loop.

    For nearly all compilers that are worth using, memset is an intrinsic function and the compiler will generate optimized, inline code for it.

    Others have suggested profiling and comparing, but I wouldn't bother. Just use memset. Code is simple and easy to understand. Don't worry about it until your benchmarks tell you this part of code is a performance hotspot.

提交回复
热议问题