一直以为strncpy效率优于snprintf. 无意中在网上看到snprintf效率大于strncpy,颇感震惊. 网址如下:
http://blog.youkuaiyun.com/wavemoon/archive/2009/12/06/4952904.aspx
后自己编码测试了下,发现情况复杂,并不是如此.
现在帖上我的测试:
环境: linux
编译器:g++
代码:
输出:
copy 102400 to 102400
snprintf time used: 455
strncpy time used: 938
copy 10240 to 10240
snprintf time used: 42
strncpy time used: 94
copy 1024 to 1024
snprintf time used: 6
strncpy time used: 10
copy 64 to 64
snprintf time used: 2
strncpy time used: 1
copy 8 to 8
snprintf time used: 2
strncpy time used: 1
copy 1024 to 10240
snprintf time used: 6
strncpy time used: 104
copy 10240 to 1024
snprintf time used: 1234
strncpy time used: 10
copy 24 to 1024
snprintf time used: 2
strncpy time used: 11
copy 1024 to 24
snprintf time used: 136
strncpy time used: 1
分析:
1. 在源串长度和目标串长度一样大的情况下,效率几乎相同. 在一个数量级上. 因为在linux下snprintf末尾会有'/0'.所以这时推荐使用snprintf
2. 源串长度 < 目标串长度 snprintf效率领先一个级别, 这时推荐使用snprintf
3. 源串长度 > 目标串长度 strncpy效率领先一个级别, 这时推荐使用strncpy
结论:
1.在源串长度 远大于 目标串长度(至少几倍). 且效率重要的时候. 考虑使用strncpy(这时别忘了在末尾加'/0')
2.其他情况推荐使用snprintf