Performance comparison of string copy functions memcpy and strncpy and snprintf _c language

Source: Internet
Author: User
Tags strlen

Problem:
Functions memcpy (dest, SRC, sizeof (dest)), strncpy (dest, SRC, sizeof (dest)) and snprintf (dest, sizeof (dest), "%s", SRC) Can copy the contents of the SRC string into the dest string.
Which way is the most efficient?
So, which way is the best performance?
Solution:
1. Establishment of three documents TEST_MEMCPY.C,TEST_STRNCPY.C and TEST_SNPRINTF.C:
File test_memcpy.c:

Copy Code code as follows:

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] = ' the ';
}
return 0;
}

File test_strncpy.c:
Copy Code code as follows:

#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;
}

File TEST_SNPRINTF.C:
Copy Code code as follows:

#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. Compile three files separately:
Copy Code code as follows:

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. Different functions consume time comparison without optimization:
Copy Code code as follows:

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$

From the results above it can be seen that, without any optimization, memcpy () and strncpy () performance difference 4 times times, snprintf () and strncpy () performance difference of about 2.5 times times.

4. Using O3 optimization of different function consumption time comparison:

Copy Code code as follows:

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$

Copy Code code as follows:

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$

From the above operation results can be seen: the use of O3 optimization, memcpy () and strncpy () performance of nearly 5 times times, snprintf () and strncpy () performance difference is almost 2.5 times times the same.

5. Performance Comparison Conclusion:
When you need to use a string copy function, never use strncpy (), whenever you use snprintf () instead, and memcpy () is a better performance implementation.
Strlen+memcpy is also how the Linux kernel is implemented.

6. Unexpected Harvest Conclusion:
Change the memset () in the three files above to use Bzero () to implement the 0 operation of the array.
Using O3 for optimization, three functions take the following time:
Copy Code code as follows:

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$

Conclusion:The performance difference between the memcpy () and the strncpy () is about 12 times times higher, while the performance difference between snprintf () and strncpy () is about 4 times times more than a 0 function.
For the 0 operation, Bzero () is far more efficient than memset ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.