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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
void random_sv1(vector<string> & sv, int size);
//randomly generate a string vector
void random_sv2(vector<string> & sv, const vector<string> & sv1);
//randomly generate a string vector, given another one, so that
//no '1's are paired with '2's
void calculate(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3);
//classic way -> character comparison
void calculate2(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3);
//smarter way -> mapping (comparison free)
void calculate3(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3);
//yet smarter way -> take advantage of the special condition that a '1' cannot be paired with a '2'
//you see, since you have this condition, you can make the comparison simpler, like this:
//if (c1=='0') (c3=c2) //since c2 >= '0'
//else c3=c1; //because if c1!='0' then c2 is EITHER '0' OR c1, both of which are <= than c1 ;)
//(simply put, the maximum element is the one that is not '0')
void calculate4(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3);
//I thought this here would be faster but it's not... :/
//I took advantage of both the special condition and the fact that the binary representations
//of '0', '1', '2' are 110000, 110001 and 110010, respectively,
//to reduce the maximum element calculation to c3=c1|c2.
void print(const vector<string> & sv);
typedef void (*calc_func)(const vector<string> &,const vector<string> &,vector<string> &);
const calc_func calc_ftable[]={calculate,calculate2,calculate3,calculate4};
int main()
{
srand(time(0));
const int size=500000;
bool print_results=false;
vector<string> sv1, sv2, sv3;
cout << "initializing string vectors...\nplease wait...\n" << endl;
random_sv1(sv1,size);
random_sv2(sv2,sv1);
int start;
int stop;
int i;
for (i=0; i<4; i++)
{
cout << "calculating with method no" << i+1 << endl;
start=clock();
cout << "start: " << start << endl;
calc_ftable[i](sv1,sv2,sv3);
stop=clock();
cout << "stop: " << stop << endl;
cout << "time taken: " << (stop-start)*1000/double(CLOCKS_PER_SEC) << " ms\n" << endl;
if (print_results)
{
cout << "sv1:\n";
print(sv1);
cout << "\nsv2:\n";
print(sv2);
cout << "\nsv3:\n";
print(sv3);
cout << endl;
}
}
cout << "hit enter to quit\n";
cin.get();
return 0;
}
void random_sv1(vector<string> & sv, int size)
{
int i;
int j;
string str;
sv.resize(size);
str.resize(10);
for (i=0; i<size; i++)
{
for (j=0; j<10; j++)
{
str[j]=rand()%3+'0';
}
sv[i]=str;
}
}
void random_sv2(vector<string> & sv, const vector<string> & sv1)
{
int i;
int j;
string str;
char c;
int temp;
int size=sv1.size();
sv.resize(size);
str.resize(10);
for (i=0; i<size; i++)
{
for (j=0; j<10; j++)
{
c=sv1[i][j];
if (c=='0') str[j]=rand()%3+'0';
else
{
temp=rand()%2;
if (c=='2') temp*=2;
str[j]=temp+'0';
}
}
sv[i]=str;
}
}
void calculate(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3)
{
int i;
int j;
string str;
char c1,c2;
int size=sv1.size();
sv3.resize(size);
str.resize(10);
for (i=0; i<size; i++)
{
for (j=0; j<10; j++)
{
c1=sv1[i][j];
c2=sv2[i][j];
if (c1>=c2) str[j]=c1;
else str[j]=c2;
}
sv3[i]=str;
}
}
void calculate2(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3)
{
static const char maximum[3][3]=
{
'0','1','2',
'1','1','2',
'2','2','2'
};
int i;
int j;
string str;
int size=sv1.size();
sv3.resize(size);
str.resize(10);
for (i=0; i<size; i++)
{
for (j=0; j<10; j++)
{
str[j]=maximum[sv1[i][j]-'0'][sv2[i][j]-'0'];
}
sv3[i]=str;
}
}
void calculate3(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3)
{
int i;
int j;
string str;
char c;
int size=sv1.size();
sv3.resize(size);
str.resize(10);
for (i=0; i<size; i++)
{
for (j=0; j<10; j++)
{
c=sv1[i][j];
if (c=='0') str[j]=sv2[i][j];
else str[j]=c;
}
sv3[i]=str;
}
}
void calculate4(const vector<string> & sv1, const vector<string> & sv2, vector<string> & sv3)
{
int i;
int j;
string str;
char c;
int size=sv1.size();
sv3.resize(size);
str.resize(10);
for (i=0; i<size; i++)
{
for (j=0; j<10; j++)
{
str[j]=(sv1[i][j])|(sv2[i][j]);
}
sv3[i]=str;
}
}
void print(const vector<string> & sv)
{
int i;
int size=sv.size();
for (i=0; i<size; i++)
{
cout << "str_vector[" << i << "]=" << sv[i] << endl;
}
}
|