1
\$\begingroup\$

In C++, I have made a basic memset / memcpy / strcpy implementation, yet I am worried that they are slower than the STL equivalent, as they are probably made in assembly, which I do not know. Is there any way of making these quicker, still using C++?

#define BYTE unsigned char
typedef BYTE byte;
void *__memset(void *_Dst, int _Val, UINT _Size)
{
 BYTE *buf = (BYTE *)_Dst;
 while (_Size--)
 {
 *buf++ = (BYTE)_Val;
 }
 return _Dst;
}
void *__memcpy(void *_Dst, const void *_Src, UINT _Size)
{
 BYTE *buf = (BYTE *)_Dst;
 BYTE *__Src = (BYTE *)_Src;
 while (_Size--)
 {
 *buf++ = *__Src++;
 }
 return _Dst;
}
char *__strcpy(char *_Dst, const char *_Src)
{
 while ((*_Dst++ = *_Src++) != '0円');
 return _Dst;
}
asked Feb 20, 2014 at 11:07
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

These are Runtime library functions, not STL (template library) functions.

The run-time library is arguably part of the language (so you can use the built-in library functions and still be "using C++").

It's possible that your compiler is able to generate these as intrinsic functions.

Things you can try to make your functions faster:

  • Use a compiler with a better optimizer
  • Copy 4 or 8 bytes at a time
  • If the buffers aren't aligned on a 4- or 8-byte boundary, copy 1 byte at a time until you come to a boundary alignment, and then copy 4 or 8 bytes at a time
answered Feb 20, 2014 at 11:32
\$\endgroup\$
1
2
\$\begingroup\$

You can test for yourself if your implementation is faster or slower than the "official" one: Simply write a test program that allocates large chunks of memory and then make a number of calls to your implementation and take the time. Then, rewrite your program to use the corresponding STL functions and repeat the time taking.

About the performance, I would assume that it is faster to operate on data types as large as the word size of your architecture (64 bit, maybe only 32 bit) instead of 8 bit only (=byte aka char). The data type "unsigned integer" should do the trick.

answered Feb 20, 2014 at 11:22
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.