字符串拷貝函數(shù)memcpy和strncpy以及snprintf 的性能比較
問題:
函數(shù)memcpy(dest, src, sizeof(dest))、strncpy(dest, src, sizeof(dest))和snprintf(dest, sizeof(dest), "%s", src)都可以將src字符串中的內(nèi)容拷貝到dest字符串中。
哪一種方式效率最高呢?
就是說,哪種方式性能最好呢?
解決辦法:
1. 建立三個文件test_memcpy.c,test_strncpy.c和test_snprintf.c:
文件test_memcpy.c:
david@u1110-hp:~/wrk/tmp/cstring$ cat test_memcpy.c
#include <string.h>
int main(){
char src[] = "1234567890";
char dest[2048];
int len = 0;
for(int i = 0; i < 10000000; ++i){
memset(dest, 0, sizeof(dest));
len = strlen(src);
len = sizeof(dest) - 1 > len? len: sizeof(dest) -1;
memcpy(dest, src, len);
dest[len] = '\0';
}
return 0;
}
文件test_strncpy.c:
#include <string.h>
int main() {
char src[] = "1234567890";
char dest[2048];
int len = 0;
for(int i = 0; i < 10000000; ++i) {
memset(dest, 0, sizeof(dest));
strncpy(dest, src, sizeof(dest));
}
return 0;
}
文件test_snprintf.c:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "1234567890";
char dest[2048];
int len = 0;
for(int i = 0; i < 10000000; ++i) {
memset(dest, 0, sizeof(dest));
snprintf(dest, sizeof(dest), "%s", src);
}
return 0;
}
2. 分別編譯三個文件:
david@u1110-hp:~/wrk/tmp/cstring$ gcc -std=c99 -o test_memcpy test_memcpy.c
david@u1110-hp:~/wrk/tmp/cstring$ gcc -std=c99 -o test_strncpy test_strncpy.c
david@u1110-hp:~/wrk/tmp/cstring$ gcc -std=c99 -o test_snprintf test_snprintf.c
3. 沒有優(yōu)化的情況下不同函數(shù)消耗時間對比:
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_strncpy
real 0m16.472s
user 0m16.309s
sys 0m0.036s
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_snprintf
real 0m6.106s
user 0m6.100s
sys 0m0.000s
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_memcpy
real 0m4.179s
user 0m4.144s
sys 0m0.000s
david@u1110-hp:~/wrk/tmp/cstring$
從上面運行結(jié)果可以看出:沒有任何優(yōu)化的情況下,memcpy()和strncpy()性能相差4倍,snprintf()和strncpy()性能相差約2.5倍。
4.采用O3優(yōu)化情況下不同函數(shù)消耗時間對比:
david@u1110-hp:~/wrk/tmp/cstring$ gcc -std=c99 -O3 -o test_snprintf test_snprintf.c
david@u1110-hp:~/wrk/tmp/cstring$ gcc -std=c99 -O3 -o test_strncpy test_strncpy.c
david@u1110-hp:~/wrk/tmp/cstring$ gcc -std=c99 -O3 -o test_memcpy test_memcpy.c
david@u1110-hp:~/wrk/tmp/cstring$
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_strncpy
real 0m16.178s
user 0m16.161s
sys 0m0.000s
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_snprintf
real 0m6.242s
user 0m6.032s
sys 0m0.056s
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_memcpy
real 0m3.567s
user 0m3.436s
sys 0m0.012s
david@u1110-hp:~/wrk/tmp/cstring$
從上面運行結(jié)果可以看出:采用O3優(yōu)化后,memcpy()和strncpy()性能相差近5倍,snprintf()和strncpy()性能相差基本不變約2.5倍。
5. 性能對比結(jié)論:
在需要用到字符串拷貝函數(shù)的時候,永遠(yuǎn)不要使用strncpy(),無論什么時候都用snprintf()來代替,而memcpy()是性能更好的實現(xiàn)方式。
strlen+memcpy也是linux內(nèi)核的實現(xiàn)方式。
6. 意外收獲結(jié)論:
將上述三個文件中的memset()改為用bzero()來實現(xiàn)數(shù)組的清零操作。
使用O3來進(jìn)行優(yōu)化,三個函數(shù)的耗時時間如下:
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_strncpy
real 0m14.395s
user 0m13.929s
sys 0m0.092s
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_snprintf
real 0m3.785s
user 0m3.772s
sys 0m0.000s
david@u1110-hp:~/wrk/tmp/cstring$ time ./test_memcpy
real 0m1.241s
user 0m1.236s
sys 0m0.004s
david@u1110-hp:~/wrk/tmp/cstring$
結(jié)論:僅僅換了一個清零函數(shù),使得memcpy()和strncpy()的性能差別達(dá)到約12倍,而snprintf()和strncpy()的性能差別也達(dá)到約4倍。
就清零操作來說,bzero()遠(yuǎn)比memset()更高效。
上一篇:引用參數(shù)和傳值參數(shù)的區(qū)別深入解析
欄 目:C語言
下一篇:undefined reference to `SetPduPowerConsumptionCnt'錯誤的解決方法
本文標(biāo)題:字符串拷貝函數(shù)memcpy和strncpy以及snprintf 的性能比較
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4256.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置