Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created October 24, 2015 20:50
Show Gist options
  • Select an option

  • Save 2bbb/2151f30736136fbdecee to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/2151f30736136fbdecee to your computer and use it in GitHub Desktop.
memset_pattern4 test
#include <iostream>
#include <chrono>
#include <string.h>
namespace laziness {
struct SimpleStopWatch {
void start() {
_start = std::chrono::high_resolution_clock::now();
}
template <typename T = std::chrono::nanoseconds>
typename T::rep rap() {
_end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<T>(_end - _start).count();
}
template <typename T = std::chrono::nanoseconds>
typename T::rep getLastRap() const {
return std::chrono::duration_cast<T>(_end - _start).count();
}
auto getLastRapMilliseconds() const
-> decltype(getLastRap<std::chrono::milliseconds>()) {
return getLastRap<std::chrono::milliseconds>();
}
auto getLastRapMicroseconds() const
-> decltype(getLastRap<std::chrono::microseconds>()) {
return getLastRap<std::chrono::microseconds>();
}
auto getLastRapNanoseconds() const
-> decltype(getLastRap<std::chrono::nanoseconds>()) {
return getLastRap<std::chrono::nanoseconds>();
}
decltype(std::chrono::high_resolution_clock::now()) _start, _end;
};
};
using namespace std;
#define w 800
#define h 600
int main(int argc, char *argv[]) {
const int loop = 1000;
{
unsigned char buf[4 * w * h];
unsigned char c[4] = {255, 255, 0, 255};
laziness::SimpleStopWatch timer;
timer.start();
for(int i = 0; i < loop; i++) {
memset_pattern4(buf, c, w * h);
}
timer.rap();
auto t = timer.getLastRapNanoseconds();
std::cout << t << std::endl;
}
{
unsigned char buf[4 * w * h];
unsigned char c[4] = {255, 255, 0, 255};
laziness::SimpleStopWatch timer;
timer.start();
size_t size = w * h;
for(int i = 0; i < loop; i++) {
for(int j = 0; j < size; j++) memcpy(buf + 4 * j, c, 4);
}
timer.rap();
auto t = timer.getLastRapNanoseconds();
std::cout << t << std::endl;
}
{
unsigned char buf[4 * w * h];
unsigned char c[4] = {255, 255, 0, 255};
laziness::SimpleStopWatch timer;
timer.start();
size_t size = w * h * 4;
for(int i = 0; i < loop; i++) {
for(int j = 0; j < size; j += 4) memcpy(buf + j, c, 4);
}
timer.rap();
auto t = timer.getLastRapNanoseconds();
std::cout << t << std::endl;
}
{
unsigned char buf[4 * w * h];
unsigned char c[4] = {255, 255, 0, 255};
laziness::SimpleStopWatch timer;
timer.start();
size_t size = w * h * 4;
for(int i = 0; i < loop; i++) {
for(int j = 0; j < size; j += 4) {
buf[j] = c[0];
buf[j + 1] = c[1];
buf[j + 2] = c[2];
buf[j + 3] = c[3];
}
}
timer.rap();
auto t = timer.getLastRapNanoseconds();
std::cout << t << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment