Yak!の日記: Expression Template による文字列の連結
日記 by
Yak!
#include <string>
#include <algorithm>
#include <iostream>
#include <boost/timer.hpp>
#include "concat.hpp"
using namespace std;
using namespace yak::concat::operators;
char* mystrcpy(char* dest, const char* src)
{
while(*dest++ = *src++) ;
return dest - 1;
}
const char str1[] = "hogehogehoge";
const char str2[] = "foobarzot";
const char str3[] = "aiueokakikukeko";
const char str4[] = "abcdefghijklmn";
const size_t len = sizeof(str1) + sizeof(str2) + sizeof(str3) + sizeof(str4) - 4;
int main(void)
{
string s;
string s1(str1);
string s2(str2);
string s3(str3);
string s4(str4);
char buf[len + 1], *buf2 = NULL;
boost::timer timer;
for(int i = 0; i < 1024*1024*10; i++) {
mystrcpy(mystrcpy(mystrcpy(mystrcpy(buf, str1), str2), str3), str4);
}
cout << "[statically allocated buffer with optimized C function]\n" << buf << ' ' << timer.elapsed()/10 << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
strcpy(buf, str1);
strcat(buf, str2);
strcat(buf, str3);
strcat(buf, str4);
}
cout << "[statically allocated buffer with standard C function]\n" << buf << ' ' << timer.elapsed()/10 << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
if(buf2) free(buf2);
buf2 = (char*)malloc(len + 1);
mystrcpy(mystrcpy(mystrcpy(mystrcpy(buf2, str1), str2), str3), str4);
}
cout << "[dynamically allocated buffer with optimized C function]\n" << buf2 << ' ' << timer.elapsed()/10 << '\n' << endl;
free(buf2);
buf2 = NULL;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
if(buf2) free(buf2);
buf2 = (char*)malloc(len + 1);
strcpy(buf2, str1);
strcat(buf2, str2);
strcat(buf2, str3);
strcat(buf2, str4);
}
cout << "[dynamically allocated buffer with standard C function]\n" << buf2 << ' ' << timer.elapsed()/10 << '\n' << endl;
free(buf2);
buf2 = NULL;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
s <<= s1 << s2 << s3 << s4;
}
cout << "[C++ string with expression template (without clear)]\n" << s << ' ' << timer.elapsed()/10 << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024; i++) {
s.clear();
s <<= s1 << s2 << s3 << s4;
}
cout << "[C++ string with expression template (with clear)]\n" << s << ' ' << timer.elapsed() << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024; i++) {
s.clear();
s = s1;
s += s2;
s += s3;
s += s4;
}
cout << "[C++ string with compound assignment operator]\n" << s << ' ' << timer.elapsed() << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024; i++) {
s.clear();
s = s1 + s2 + s3 + s4;
}
cout << "[C++ string with add operator]\n" << s << ' ' << timer.elapsed() << '\n' << endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <boost/timer.hpp>
#include "concat.hpp"
using namespace std;
using namespace yak::concat::operators;
char* mystrcpy(char* dest, const char* src)
{
while(*dest++ = *src++) ;
return dest - 1;
}
const char str1[] = "hogehogehoge";
const char str2[] = "foobarzot";
const char str3[] = "aiueokakikukeko";
const char str4[] = "abcdefghijklmn";
const size_t len = sizeof(str1) + sizeof(str2) + sizeof(str3) + sizeof(str4) - 4;
int main(void)
{
string s;
string s1(str1);
string s2(str2);
string s3(str3);
string s4(str4);
char buf[len + 1], *buf2 = NULL;
boost::timer timer;
for(int i = 0; i < 1024*1024*10; i++) {
mystrcpy(mystrcpy(mystrcpy(mystrcpy(buf, str1), str2), str3), str4);
}
cout << "[statically allocated buffer with optimized C function]\n" << buf << ' ' << timer.elapsed()/10 << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
strcpy(buf, str1);
strcat(buf, str2);
strcat(buf, str3);
strcat(buf, str4);
}
cout << "[statically allocated buffer with standard C function]\n" << buf << ' ' << timer.elapsed()/10 << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
if(buf2) free(buf2);
buf2 = (char*)malloc(len + 1);
mystrcpy(mystrcpy(mystrcpy(mystrcpy(buf2, str1), str2), str3), str4);
}
cout << "[dynamically allocated buffer with optimized C function]\n" << buf2 << ' ' << timer.elapsed()/10 << '\n' << endl;
free(buf2);
buf2 = NULL;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
if(buf2) free(buf2);
buf2 = (char*)malloc(len + 1);
strcpy(buf2, str1);
strcat(buf2, str2);
strcat(buf2, str3);
strcat(buf2, str4);
}
cout << "[dynamically allocated buffer with standard C function]\n" << buf2 << ' ' << timer.elapsed()/10 << '\n' << endl;
free(buf2);
buf2 = NULL;
timer.restart();
for(int i = 0; i < 1024*1024*10; i++) {
s <<= s1 << s2 << s3 << s4;
}
cout << "[C++ string with expression template (without clear)]\n" << s << ' ' << timer.elapsed()/10 << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024; i++) {
s.clear();
s <<= s1 << s2 << s3 << s4;
}
cout << "[C++ string with expression template (with clear)]\n" << s << ' ' << timer.elapsed() << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024; i++) {
s.clear();
s = s1;
s += s2;
s += s3;
s += s4;
}
cout << "[C++ string with compound assignment operator]\n" << s << ' ' << timer.elapsed() << '\n' << endl;
timer.restart();
for(int i = 0; i < 1024*1024; i++) {
s.clear();
s = s1 + s2 + s3 + s4;
}
cout << "[C++ string with add operator]\n" << s << ' ' << timer.elapsed() << '\n' << endl;
return 0;
}
Expression Template による文字列の連結 More ログイン