1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
#include <ctime>
#include <cmath>
using namespace std;
const int num =1000;
template<typename T>
struct image
{
image(int hh,int ww)
:h(hh),w(ww),buffer(hh*ww)
{};
int h;
int w;
vector<T> buffer;
};
struct pix
{
pix():r(0),g(0),b(0)
{};
pix(char rr,char gg,char bb)
:r(rr),g(gg),b(bb)
{}
char r;
char g;
char b;
pix& operator*=(int a)
{
r*=a;
g*=a;
b*=a;
return *this;
}
};
struct gen
{
char operator()()
{
return rand()%256+1;
}
};
struct genpix
{
pix operator()()
{
pix p;
p.r = rand()%256+1;
p.g = rand()%256+1;
p.b = rand()%256+1;
return p;
}
};
template <typename T>
struct multiplie
{
multiplie(int aa):a(aa){};
void operator()(T & pix)
{
pix*=a;
}
int a;
};
struct MIN
{
MIN():min(255){};
void operator()(char pix)
{
if (pix<min) min = pix;
}
char min;
};
struct MINPIX
{
MINPIX()
:min(255,255,255)
{};
void operator()(const pix & pix)
{
if (pix.r<min.r) min.r = pix.r;
if (pix.g<min.g) min.g = pix.g;
if (pix.b<min.b) min.b = pix.b;
}
pix min;
};
int main(int argc, char* argv[])
{
srand(clock());
image<char> img(1000,1000);
std::generate
(
img.buffer.begin(),
img.buffer.end(),
gen()
);
image<pix> imgRGB(1000,1000);
std::generate
(
imgRGB.buffer.begin(),
imgRGB.buffer.end(),
genpix()
);
while(1)
{
clock_t t0 = clock();
for (int i =0;i<num;++i)
{
std::for_each
(
img.buffer.begin(),
img.buffer.end(),
multiplie<char>(2)
);
}
double tempA = double(clock()-t0)/num;
std::cout<<"************MULTIPLICATION**************" <<std::endl;
std::cout<<"grayscale"" "<<tempA <<std::endl;
t0 = clock();
for (int i =0;i<num;++i)
{
std::for_each
(
imgRGB.buffer.begin(),
imgRGB.buffer.end(),
multiplie<pix>(2)
);
}
double tempB = double(clock()-t0)/num;
std::cout<<"rgb"<<" "<<tempB <<std::endl;
std::cout<<"rgb/grayscale"<<" "<<100*(tempB/tempA)<<"%"<<std::endl;
t0 = clock();
int min;
for (int i =0;i<num;++i)
{
min = std::for_each
(
img.buffer.begin(),
img.buffer.end(),
MIN()
).min;
}
tempA = double(clock()-t0)/num;
std::cout<<"************MIN**************" <<std::endl;
std::cout<<"grayscale"" "<<tempA <<std::endl;
t0 = clock();
pix minpix;
for (int i =0;i<num;++i)
{
minpix = std::for_each
(
imgRGB.buffer.begin(),
imgRGB.buffer.end(),
MINPIX()
).min;
}
tempB = double(clock()-t0)/num;
std::cout<<"rgb "<<minpix.r<<" "<<minpix.g<<" "<<minpix.b<<" "<<tempB <<std::endl;
std::cout<<"B/A"<<" "<<100*(tempB/tempA)<<"%"<<std::endl;
}
return 0;
} |
Partager