C++には文字の分割で気の利いた方法がない。vectorで結果を受け取る方法は、その過程でメモリの
やり取りに無駄が多い。
そこで、SplitStringというクラスを作成して、試してみるとなんと3倍も速くなった。


#include "stdafx.h"
#include
#include
#include
#include

using namespace std;

typedef void (*Func1)();

double Task1( Func1 f );

void func1();
void func2();

int _tmain(int argc, _TCHAR* argv[])
{
_tsetlocale(0,L"");

std::wcout << L". 経過時間 " << Task1( func1 ) << L"s \n"; // 0.21s
std::wcout << L". 経過時間 " << Task1( func2 ) << L"s \n"; // 0.07s

return 0;
}
double Task1( Func1 f )
{
LARGE_INTEGER ntime,freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&ntime);

f();

LARGE_INTEGER ntime2;
QueryPerformanceCounter(&ntime2);
double duration =(((double )( ntime2.QuadPart-ntime.QuadPart) / ( double)freq.QuadPart)); // 経過時間 秒単位
return duration;
}

// ありがちな文字の分割方法
vector split(const wstring &str, WCHAR delim){
vector res;
size_t current=0, found;
while( (found=str.find_first_of(delim, current)) != wstring::npos)
 {
res.push_back(wstring(str, current, found - current));
current = found + 1;
}
res.push_back(wstring(str, current, str.size() - current));
return res;
}

void func1()
{
std::vector ar;
for( int i = 0; i < 1000; i++ )
{
ar.clear();
ar = split( L"If you say you need it, It's all for you, If you gotta have it, It's all for you", L' ');
}

std::wcout << ar[6].c_str() << L" " << ar[16].c_str()<< L" " << ar[8].c_str()<< L" " << ar[18].c_str();
}
void func2()
{
V4::SplitString cc;
for( int i = 0; i < 1000; i++ )
{
cc.clear();
int cnt = cc.Split( L"If you say you need it, It's all for you, If you gotta have it, It's all for you", L' ');
}

std::wcout << cc[6] << L" " << cc[16]<< L" "<< cc[8]<< L" "<< cc[18] ;
}