Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/4362020 to your computer and use it in GitHub Desktop.
Save usagi/4362020 to your computer and use it in GitHub Desktop.
C++ vs. C ; R5G6B5 color pixel transform battle source (1)

Article

http://usagi.hatenablog.jp/entry/2012/12/23/181513

build eg.

$ cc master.c -o master_c.O0 -O0
$ cc master_macro.c -o master_macro_c.O0 -O0
$ g++ -std=c++11 master_fixed.cxx -o master_fixed_cxx.O0 -O0

dependency

  • master_fixed_cxx: boost (it can replace std)

Copyrights and Licenses

A PART OF MASTER SOURCE

(C) 2012 池田 公平. Allrights reserved.

ANOTHER PARTS

(C) 2012 Usagi Ito / Wonder Rabbit Project.

CC0
To the extent possible under law, Usagi Ito has waived all copyright and related or neighboring rights to this work. This work is published from: 日本.

#include <malloc.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#define IMAGE_HEIGHT 1920
#define IMAGE_WIDTH 1080
// NOTE: THERE ARE BAD DEFINE USAGE.
#define short uint16_t
#define int unsigned
// BEGIN OF MASTER SOURCE
int get_r(short x) { return ((x >> 8) & 0xf8); }
int get_g(short x) { return ((x >> 3) & 0xfc); }
int get_b(short x) { return ((x << 3) & 0xff); }
short to_rgb(int r, int g, int b) {
return (short)(((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3));
}
void test_bitmap_color_transform_pure_c(short* buffer) {
short* p = buffer;
int n;
for (n = 0; n < IMAGE_HEIGHT * IMAGE_WIDTH; ++n, ++p) {
int r = get_r(*p);
int g = get_g(*p);
int b = get_b(*p);
*p = to_rgb(g, b, r);
}
}
// END OF MASTER SOURCE
// NOTE: UNDEFINE BAD DEFINES.
#undef int
#undef short
#define NUM_OF_LOOP 1000
int main(){
uint16_t* pixels = calloc(
IMAGE_WIDTH * IMAGE_HEIGHT,
sizeof( uint16_t )
);
unsigned n;
clock_t t, tt;
setvbuf(stdout, NULL, _IONBF, 0);
puts("last:");
for(n = NUM_OF_LOOP, tt = 0; n; --n){
t = clock();
test_bitmap_color_transform_pure_c( pixels );
tt += clock() - t;
printf("\x1b[12D %08X", n);
}
puts("\x1b[12D FINISHED\n");
free(pixels);
printf(
"result: %.3e [sec/function]\n",
(double)tt / CLOCKS_PER_SEC / NUM_OF_LOOP
);
return 0;
}
#include <vector>
#include <boost/range/algorithm/for_each.hpp>
#include <chrono>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace boost;
// BEGIN OF MASTER SOURCE
template <int PixelBits>
struct pixel_t {};
template <>
struct pixel_t<16> {
uint16_t value_;
pixel_t() : value_(0) {}
pixel_t(int r, int g, int b)
: value_(static_cast<uint16_t>(((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3)))
{}
int get_r() const { return (value_ >> 8) & 0xf8; }
int get_g() const { return (value_ >> 3) & 0xfc; }
int get_b() const { return (value_ << 3) & 0xff; }
};
struct transcolor {
template <typename Pixel>
void operator () (Pixel& px) {
px = Pixel(px.get_g(), px.get_b(), px.get_r());
}
};
void test_bitmap_color_transform_cpp(vector< pixel_t<16> >& buffer){
for_each(buffer, transcolor());
}
// END OF MASTER SOURCE
namespace{
constexpr size_t image_width = 1920;
constexpr size_t image_height = 1080;
constexpr auto num_of_loop = 1000ull;
}
int main(){
vector<pixel_t<16>> pixels;
pixels.resize(image_width * image_height);
using c = chrono::high_resolution_clock;
c::duration tt;
cout << "last:\n";
for(auto n = num_of_loop; n; --n){
auto t = c::now();
test_bitmap_color_transform_cpp(pixels);
tt += c::now() - t;
cout
<< "\x1b[12D "
<< hex
<< setw(8)
<< setfill('0')
<< n
<< flush
;
}
cout
<< "\x1b[12D FINISHED"
<< endl;
cout
<< "result: "
<< scientific
<< setprecision(3)
<< chrono::duration_cast<chrono::duration<double>>(tt).count() / num_of_loop
<< endl;
}
#include <malloc.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#define IMAGE_HEIGHT 1920
#define IMAGE_WIDTH 1080
// NOTE: THERE ARE BAD DEFINE USAGE.
#define short uint16_t
#define int unsigned
// BEGIN OF MASTER SOURCE
#define GET_R(x) ((x >> 8) & 0xf8)
#define GET_G(x) ((x >> 3) & 0xfc)
#define GET_B(x) ((x << 3) & 0xff)
#define TO_RGB(r, g, b) (((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3))
void test_bitmap_color_transform_macro_c(short* buffer) {
short* p = buffer;
int n;
for (n = 0; n < IMAGE_HEIGHT * IMAGE_WIDTH; ++n, ++p) {
int r = GET_R(*p);
int g = GET_G(*p);
int b = GET_B(*p);
*p = (short)TO_RGB(g, b, r);
}
}
// END OF MASTER SOURCE
// NOTE: UNDEFINE BAD DEFINES.
#undef int
#undef short
#define NUM_OF_LOOP 1000
int main(){
uint16_t* pixels = calloc(
IMAGE_WIDTH * IMAGE_HEIGHT,
sizeof( uint16_t )
);
unsigned n;
clock_t t, tt;
setvbuf(stdout, NULL, _IONBF, 0);
puts("last:");
for(n = NUM_OF_LOOP, tt = 0; n; --n){
t = clock();
test_bitmap_color_transform_macro_c( pixels );
tt += clock() - t;
printf("\x1b[12D %08X", n);
}
puts("\x1b[12D FINISHED\n");
free(pixels);
printf(
"result: %.3e [sec/function]\n",
(double)tt / CLOCKS_PER_SEC / NUM_OF_LOOP
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment