Friday, January 26, 2018

memcpy vs strcpy performance

I have made a memcpy vs strcpy performance comparison test. It helps to understand ways to optimize Shuf project.

My results:

memcpy
execution time 0.107s (8.7 GB/s)
strcpy
execution time 0.183 (5.1 GB/s)

I have test it with Clang 6.0 and gcc 5.4 and found no big dfference.  

source code:
std::vector<char> buf(1000000000);
size_t bufSize = buf.size();

std::cerr<< "generating\n";
std::generate(begin(buf), end(buf), std::rand); 
std::cerr<< "done\n";
std::vector<size_t> bufLen;
size_t maxLen = 0;
for(size_t i=0;i<bufSize;) {
  char* ptr = (char*)memchr(&buf[0]+i, 0, bufSize-i);
  if(!ptr) break;
  size_t len = ptr - &buf[0] - i + 1;
  bufLen.push_back(len);
  i+=len;
  maxLen=std::max(maxLen, len);
}

clock_t c1 = clock();

std::vector<char> tempBuf(maxLen);
size_t total = bufLen.size();

//memcpy
char* ptr = &buf[0];
for(auto it: bufLen) {
  memcpy(&tempBuf[0], ptr, it);
  ptr+=it;
}

clock_t c2 = clock();
std::cerr<< "memcpy " << (double(c2 - c1) / CLOCKS_PER_SEC) << "\n";

//strcpy
ptr = &buf[0];
for(auto it: bufLen) {
  auto ret = strcpy(&tempBuf[0], ptr);
  ptr+=it;
}

clock_t c3 = clock();
std::cerr<< "strcpy " << (double(c3 - c2) / CLOCKS_PER_SEC) << "\n";

The following script extracts all audio streams from files in current directory

The following script extracts all audio streams from files in current directory ls |parallel "ffmpeg -i {} 2>&1 |\ sed -n &...