memcpy 和 snprintf的效率比較
阿新 • • 發佈:2018-11-25
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char **argv) { char *name="17008.1000.32.34.01.11"; char operator[32] = {0}; char str_out[64] = {0}; unsigned long long i = 0; for(i=0;i<1000000000;i++) { //memcpy(operator, name+11, 2); snprintf(operator,2,"%s",name+11); } printf("name:%s,operator:%s\n",name,operator); printf("atoi=%d\n",atoi(operator)); switch(atoi(operator)) { case 00: snprintf(str_out, sizeof(str_out), "L718-CN-00"); break; case 32: snprintf(str_out, sizeof(str_out), "L718-CN-01"); break; case 31: snprintf(str_out, sizeof(str_out), "L718-CN-02"); break; case 33: snprintf(str_out, sizeof(str_out), "L718-CN-06"); break; } printf("%s\n",str_out); //printf("we use memcpy\n"); printf("we use snprintf\n"); return 0; }
用虛擬機器分別執行程式,統計時間,發現memcpy的效率比snprintf高很多