본문으로 바로가기

다음과 같은.. 스트링이 있을때..
char *s = "10000001   1201712009430120094781202507512025075";
8,8,8,8,8,8,8 자리로 끊어서 읽고 싶은데..
sscanf는 공백을 무시고 읽어.. 원하는데로 동작되지 않음..
하여 찾다보니.. boost에 offset_separator 라는것이 있음..
테스트 하는 김에 속도 비교를 해봄..

sscanf의 경우.. (대략 88.637946초)

std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
for(int i=0; i<100000000; i++) {
    char *s = "10000001   1201712009430120094781202507512025075";
    unsigned int eid, pid, n1, n2, n3, n4;
    int cnt = sscanf(s, "%8u%8u%8u%8u%8u%8u", &eid, &pid, &n1, &n2, &n3, &n4);
}
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
std::chrono::duration<double> sec = end - start;
printf("%f(sec)}", sec.count());

boost의 offset_separator (대략 72.378287초)

std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
for(int i=0; i<100000000; i++) {
    std::string s = "10000001   1201712009430120094781202507512025075";
 
   int offsets[] = {8,8,8,8,8,8};
   boost::offset_separator f(offsets, offsets+6);
   boost::tokenizer<boost::offset_separator> tok(s,f);
   //for(boost::tokenizer<boost::offset_separator>::iterator beg=tok.begin(); beg!=tok.end();++beg){
     //  std::cout << *beg << "\n";
  // }
   const size_t count = std::distance(tok.begin(), tok.end());
   //std::cout << count << "\n";
   boost::tokenizer<boost::offset_separator>::iterator it1 = tok.begin();
   //std::cout << *it1 << "\n";
   ++it1;
   //std::cout << *it1 << "\n";
   ++it1;
   //std::cout << *it1 << "\n";
   ++it1;
   //std::cout << *it1 << "\n";
   ++it1;
   //std::cout << *it1 << "\n";
   ++it1;
   //std::cout << *it1 << "\n";
}
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
std::chrono::duration<double> sec = end - start;
printf("%f(sec)}", sec.count());

역시.. 구글링을 해보면 sscanf의 속도가 느리다고 하는데.. 맞는가봄..
일단 boost의 경우 자리수를 지정해서 그 만큼 짤라내는게 아주 잘 되고..
sscanf보다 빠르니.. 요걸 쓰기로 결정..