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
|
#include <iostream>
#include <algorithm>
#include <string>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <windows.h>
#include <time.h>
struct IsCharListCamboui
{
const char *s_cp;
size_t s_cplen;
IsCharListCamboui(const char *cp , size_t cplen)
:s_cp(cp),s_cplen(cplen) {}
bool operator()(char c)
{
return (std::char_traits<char>::find(s_cp,s_cplen,c)!=0);
}
};
struct IsCharListMe
{
public:
IsCharListMe(const std::string& charList) : charList_(charList) {}
bool operator()(char c)
{
return (charList_.find(c) != std::string::npos);
}
private:
const std::string& charList_;
};
void removeCharListCamboui1(std::string& s)
{
std::string::iterator f, t, end;
for (f = t = s.begin(), end = s.end() ; f != end ; ++f)
{
char c = *f;
if (memchr("1234", c, 4) == 0)
*t++ = c;
}
s.erase(t, end);
}
inline void removeCharListCamboui2(std::string& s)
{
s.erase( std::remove_if(s.begin(),s.end(), IsCharListCamboui("1234", 4)) ,s.end());
}
inline void removeCharListMe(std::string& s)
{
s.erase( std::remove_if(s.begin(),s.end(), IsCharListMe("1234")) ,s.end());
}
inline void removeCharListBoost1(std::string& s)
{
s.erase( std::remove_if(s.begin(), s.end(), boost::is_any_of("1234")), s.end());
}
inline void removeCharListBoost2(std::string& s)
{
boost::erase_all_regex(s, boost::regex("[1234]"));
}
std::string ul2string(unsigned long value)
{
char buf[12];
char *bufp=buf;
for(;;)
{
*bufp++=(char)(value % 10u);
if ((value/=10u)==0ul)
break;
}
std::string s;
s.resize(bufp-buf);
std::string::iterator it_s=s.begin();
while (bufp!=buf)
{
char c=*--bufp;
*it_s=(char)(c+'0');
++it_s;
}
return s;
}
int main()
{
srand(time(NULL));
LARGE_INTEGER ticksPerSecond;
LARGE_INTEGER start; // A point in time
LARGE_INTEGER end; // For converting tick into real time
double diff;
// get the high resolution counter's accuracy
QueryPerformanceFrequency(&ticksPerSecond);
std::string s;
// test 1
QueryPerformanceCounter(&start);
for(int i = 0 ; i < 10000000 ; i++)
{
s = ul2string(rand());
}
QueryPerformanceCounter(&end);
diff = ((end.QuadPart - start.QuadPart) * 1000.0) / ticksPerSecond.QuadPart;
std::cout << "Test Temoin (pas de traitement) : " << diff << std::endl;
// test 1
QueryPerformanceCounter(&start);
for(int i = 0 ; i < 10000000 ; i++)
{
s = ul2string(rand());
removeCharListCamboui1(s);
}
QueryPerformanceCounter(&end);
diff = ((end.QuadPart - start.QuadPart) * 1000.0) / ticksPerSecond.QuadPart;
std::cout << "Test Camboui 1 (acces direct par iterateur) : " << diff << std::endl;
// test 2
QueryPerformanceCounter(&start);
for(int i = 0 ; i < 10000000 ; i++)
{
s = ul2string(rand());
removeCharListCamboui2(s);
}
QueryPerformanceCounter(&end);
diff = ((end.QuadPart - start.QuadPart) * 1000.0) / ticksPerSecond.QuadPart;
std::cout << "Test camboui 2 (avec foncteur + char*) : " << diff << std::endl;
//test 3
QueryPerformanceCounter(&start);
for(int i = 0 ; i < 10000000 ; i++)
{
s = ul2string(rand());
removeCharListMe(s);
}
QueryPerformanceCounter(&end);
diff = ((end.QuadPart - start.QuadPart) * 1000.0) / ticksPerSecond.QuadPart;
std::cout << "Test me (avec foncteur + string) : " << diff << std::endl;
//test 4
QueryPerformanceCounter(&start);
for(int i = 0 ; i < 10000000 ; i++)
{
s = ul2string(rand());
removeCharListBoost1(s);
}
QueryPerformanceCounter(&end);
diff = ((end.QuadPart - start.QuadPart) * 1000.0) / ticksPerSecond.QuadPart;
std::cout << "Test boost (boost::is_any_of) : " << diff << std::endl;
//test 5
QueryPerformanceCounter(&start);
for(int i = 0 ; i < 1000000 ; i++)
{
s = ul2string(rand());
removeCharListBoost2(s);
}
QueryPerformanceCounter(&end);
diff = ((end.QuadPart - start.QuadPart) * 1000.0) / ticksPerSecond.QuadPart;
std::cout << "Test boost regex : " << diff << std::endl;
return 0;
} |
Partager