fork(3) download
#include <string.h>
 
#include <iostream>
#include <chrono>
#include <iostream>
#include <iomanip>
 
using namespace std::chrono;
 
// based on
// http://w...content-available-to-author-only...t.org/doc/libs/1_54_0/libs/chrono/example/await_keystroke.cpp
template<class Clock>
class Timer
{
typename Clock::time_point start;
 
public:
Timer()
: start(Clock::now()) {
}
 
typename Clock::duration elapsed() const {
return (Clock::now() - start);
}
 
double seconds() const {
return (elapsed().count() *
((double)Clock::period::num / Clock::period::den));
}
};
 
#define BUFFERSIZE (100 * 1024 * 1024) // 100 mb
#define LOOPS 10
#define MAXSTRIDE 128
 
int main()
{
char *b1 = (char *)::malloc(BUFFERSIZE + MAXSTRIDE);
char *b2 = (char *)::malloc(BUFFERSIZE + MAXSTRIDE);
::memset(b1, 0, BUFFERSIZE + MAXSTRIDE);
 
// Each test is performed |LOOP| times; the average run-time is then printed.
 
 // Test memcpy() by copying from buffer b1 to b2
{Timer<high_resolution_clock> t;
for (int i = 0; i < LOOPS; i++)
memcpy(b2, b1, BUFFERSIZE);
double seconds = t.seconds();
std::cout << "memcpy " << (seconds / LOOPS) << std::endl;
}
// Test memcpy() by copying from buffer b1 to b2
Timer<high_resolution_clock> t;
for (int i = 0; i < LOOPS; i++)
memcpy(b2, b1, BUFFERSIZE);
double seconds = t.seconds();
std::cout << "memcpy " << (seconds / LOOPS) << std::endl;

// Test memmove() by inserting a small gap at the beginning of the buffer
for (int stride = 2; stride <= MAXSTRIDE; stride *= 2) {
Timer<high_resolution_clock> t;
for (int i = 0; i < LOOPS; i++)
memmove(b2, b1, BUFFERSIZE);
double seconds = t.seconds();
std::cout << "memmove (" << std::setfill('0') << std::setw(3)
<< stride << ") " << (seconds / LOOPS) << std::endl;
}
 
::free(b1);
::free(b2);
} 
Time limit exceeded #stdin #stdout 5s 207936KB
stdin
Standard input is empty
stdout
memcpy 0.0688002
memcpy 0.0583162
memmove (002) 0.0577443
memmove (004) 0.05862
memmove (008) 0.0601029
memmove (016) 0.0601265
memmove (032) 0.0592741
memmove (064) 0.0604098