Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State of "memset" functionality in C++ with modern compilers

Context:

A while ago, I stumbled upon this 2001 DDJ article by Alexandrescu: http://www.ddj.com/cpp/184403799

It's about comparing various ways to initialized a buffer to some value. Like what "memset" does for single-byte values. He compared various implementations (memcpy, explicit "for" loop, duff's device) and did not really find the best candidate across all dataset sizes and all compilers.

Quote:

There is a very deep, and sad, realization underlying all this. We are in 2001, the year of the Spatial Odyssey. (...) Just step out of the box and look at us — after 50 years, we're still not terribly good at filling and copying memory.

Question:

  1. does anyone have more recent information about this problem ? Do recent GCC and Visual C++ implementations perform significantly better than 7 years ago ?
  2. I'm writing code that has a lifetime of 5+ (probably 10+) years and that will process arrays' sizes from a few bytes to hundred of megabytes. I can't assume that my choices now will still be optimal in 5 years. What should I do:
    • a) use the system's memset (or equivalent) and forget about optimal performance or assume the runtime and compiler will handle this for me.
    • b) benchmark once and for all on various array sizes and compilers and switch at runtime between several routines.
    • c) run the benchmark at program initialization and switch at runtime based on accurate (?) data.

Edit: I'm working on image processing software. My array items are PODs and every millisecond counts !

Edit 2: Thanks for the first answers, here are some additional informations:

  • Buffer initialization may represent 20%-40% of total runtime of some algorithms.
  • The platform may vary in the next 5+ years, although it will stay in the "fastest CPU money can buy from DELL" category. Compilers will be some form of GCC and Visual C++. No embedded stuff or exotic architectures on the radar
  • I'd like to hear from people who had to update their software when MMX and SSE appeared, since I'll have to do the same when "SSE2015" becomes available... :)
like image 929
rlerallut Avatar asked Oct 05 '08 12:10

rlerallut


People also ask

Why memset is used in C?

In C, the memset() function is used to set a one-byte value to a memory block byte by byte. This function is useful for initialization of a memory block byte by byte by a particular value.

What is memset and memcpy in C?

memset() is used to set all the bytes in a block of memory to a particular char value. Memset also only plays well with char as it's its initialization value. memcpy() copies bytes between memory. This type of data being copied is irrelevant, it just makes byte-for-byte copies.

When should I use memset?

memset() is used to fill a block of memory with a particular value. The syntax of memset() function is as follows : // ptr ==> Starting address of memory to be filled // x ==> Value to be filled // n ==> Number of bytes to be filled starting // from ptr to be filled void *memset(void *ptr, int x, size_t n);


1 Answers

The DDJ article acknowledges that memset is the best answer, and much faster than what he was trying to achieve:

There is something sacrosanct about C's memory manipulation functions memset, memcpy, and memcmp. They are likely to be highly optimized by the compiler vendor, to the extent that the compiler might detect calls to these functions and replace them with inline assembler instructions — this is the case with MSVC.

So, if memset works for you (ie. you are initializing with a single byte) then use it.

Whilst every millisecond may count, you should establish what percentage of your execution time is lost to setting memory. It is likely very low (1 or 2%??) given that you have useful work to do as well. Given that the optimization effort would likely have a much better rate of return elsewhere.

like image 61
Rob Walker Avatar answered Nov 05 '22 05:11

Rob Walker