ÇØ½¬, ÄÁÅ×À̳Ê, Å×À̺í, º¥Ä¡¸¶Å©
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

Contents

1 ÇØ½Ã´Â Á¤¸»·Î ºü¸¥ °ÍÀÎÁö?
1.1 int
1.1.1 Dinkum
1.1.2 STLport
1.1.3 SourcePro/Core
1.2 string
1.2.1 Dinkum
1.2.2 STLport
1.2.3 SourcePro/Core


1 ÇØ½Ã´Â Á¤¸»·Î ºü¸¥ °ÍÀÎÁö?


Ç¥ÁØ C++ÇÁ·Î±×·¥ ¶óÀ̺귯¸®°¡ Á¦°øÇÏ´Â ÄÁÅ×ÀÌ³Ê set/map(multiset/multimap)´Â Åë»ó 2ÁøÆ®¸®(binary-tree)·Î ¼³°èµÇ¾î ÀÖ½À´Ï´Ù. ¿ä¼ÒÀÇ ´ë¼Ò °ü°è¿¡ ±Ù°ÅÇØ Æ®¸®¸¦ °ü¸®ÇÏ¿© »ðÀÔ/»èÁ¦/°Ë»ö¿¡ ÇÊ¿äÇÑ ½Ã°£ °è»ê·®À» Àý¾àÇϵµ·Ï ¸¸µé¾îÁø ¶Ù¾î³­ ±¸Á¶ÀÔ´Ï´Ù.

±×·³ Á» ´õ °í¼ÓÀÇ ÄÁÅ×À̳ʴ ¾ø´Â °ÍÀϱî¿ä? ÇØ½ÃÇ¥¿¡ ÀÇÇÑ ÄÁÅ×À̳ʰ¡ ÇϳªÀÇ ¹æ¹ýÀÔ´Ï´Ù. ÇØ½ÃÇ¥ÀÇ »çÀÌÁî¿Í ÇØ½¬ ÇÔ¼ö¸¦ ÀûÀýÈ÷ ¼±ÅÃÇϸé Á» ´õ ºü¸£°Ô Á¢±ÙÇÒ ¼ö°¡ ÀÖ½À´Ï´Ù.

±×·¯³ª À¯°¨½º·´Áö¸¸ Ç¥ÁØ C++ ¿¡´Â ÇØ½Ã¡¤ÄÁÅ×À̳ʴ ¹Þ¾Æµé¿©ÁöÁö ¾Ê¾Ò½À´Ï´Ù. ´ÙÇེ·´°Ôµµ Ç¥ÁØÀº µÇÁö ¾Ê±â´Â ÇßÁö¸¸, ÇØ½Ã¡¤ÄÁÅ×À̳ʴ ¸î°³ÀÇ ±¸ÇöÀÌ ÀÌ·ç¾îÁö°í ÀÖ½À´Ï´Ù.

±× Áß¿¡ ´ëÇ¥ÀûÀÎ ÇØ½Ã¡¤ÄÁÅ×À̳ʵéÀÇ ±¸Çö¿¡ ´ëÇØ, ½ÇÁ¦·Î ±× ÆÛÆ÷¸Õ½º ÃøÁ¤À» ½ÃµµÇß½À´Ï´Ù.

ÃøÁ¤ ÇÑ ÇØ½Ã¡¤ÄÁÅ×À̳ʴ ¾Æ·¡ÀÇ 3Á¾·ùÀÔ´Ï´Ù:

Dinkum Standard Library 2.33 (Dinkumware ) Visual C++¿¡ OEM Á¦°øµÇ°í ÀÖ´Â STLÀÇ Á¦Ç°ÀÔ´Ï´Ù.
STLport 4.5. 1 (STLport ) SGIÆÇÀ» º£À̽º·Î, °¢Á¾ OS/ÄÄÆÄÀÏ·¯·Î »ç¿ëÇÒ ¼ö ÀÖµµ·Ï ¼öÁ¤ÀÌ ´õÇØÁö°í ÀÖ½À´Ï´Ù.
SourcePro/Core (Rogue Wave ) C++ Builder¿¡ OE°ø±ÞµÇ°í ÀÖ´Â STLÀÇ Á¦Ç°ÆÇÀÔ´Ï´Ù.

Windows2000, Visual C++ 6.0ÀÇ È¯°æ¿¡¼­ ¾Æ·¡ÀÇ Äڵ带 multi-thread/DLL·Î ÄÄÆÄÀÏ ÇÏ¿© ÃøÁ¤Çß½À´Ï´Ù.

1.1 int


 
#include <windows.h> // GetTickCount 
#include <iostream>  // cout 
#include <set>       // set 
 
#if   defined(BENCH_DINKUM) 
#include <hash_set> 
 
typedef std::hash_set<int> hashset_type; 
 
#elif defined(BENCH_STLPORT) 
#include <hash_set> 
 
typedef std::hash_set<int> hashset_type; 
 
#elif defined(BENCH_SOURCEPRO) 
#include <rw/stdex/hashset.h> 
struct inthash { 
  unsigned long operator()(int x) const { return x; } 
}; 
 
typedef rw_hashset<int,inthash,std::equal_to<int>,std::allocator<int> > hashset_type; 
 
#endif 
 
template<class C> 
void bench(const char* msg, C& c, size_t N) { 
  std::cout << msg << '(' << N << ')'; 
  int i; 
  long tick = GetTickCount(); 
  for ( i = 0; i < N; ++i ) 
    c. insert(i); 
  for ( i = 0; i < N; ++i ) 
    c. erase(i); 
  tick = GetTickCount() - tick; 
  std::cout << tick << "[ms], " << (double)tick/(double)N << "[ms/element]" << std::endl; 
} 
 
int main() { 
  for ( size_t n = 1000; n < 40000; n += n ) { 
    hashset_type hs; 
    bench("hash table", hs, n); 
    std::set<int> s; 
    bench("bin.   tree", s, n); 
  } 
  return 0; 
} 
 


ÇÔ¼ö bench´Â int¸¦ ¿ä¼ÒÀÇ ÇüÅ·ΠÇÏ´Â ÄÁÅ×À̳ʿ¡[0, N)ÀÇ ¹üÀ§ÀÇ N°³ÀÇ ¿ä¼Ò¸¦ »ðÀÔ/»èÁ¦ÇØ, °Å±â¿¡ ÇÊ¿ä·Î ÇÏ´Â ½Ã°£ ¹× ¿ä¼Ò 1°³ ´çÀÇ ½Ã°£À» Ãâ·ÂÇÕ´Ï´Ù.

main¿¡¼­´Â ¿ä¼Ò¼ö 1000, 2000, 4000, ... 32000ÀÇ °æ¿ì¿¡ ´ëÇØ ÇØ½Ã¡¤ÄÁÅ×ÀÌ³Ê¿Í 2ÁøÆ®¸® ÄÁÅ×À̳ʸ¦ bench¿¡°Ô ÁÖ¾î °¢°¢À» ºñ±³Çϰí ÀÖ½À´Ï´Ù.

1.1.1 Dinkum

½ÇÇà °á°ú  
 
hash table(1000)10[ms], 0.01[ms/element] 
bin.   tree(1000) 10[ms], 0.01[ms/element] 
hash table(2000)30[ms], 0.015[ms/element] 
bin.   tree(2000) 10[ms], 0.005[ms/element] 
hash table(4000)100[ms], 0.025[ms/element] 
bin.   tree(4000) 20[ms], 0.005[ms/element] 
hash table(8000)371[ms], 0.046375[ms/element] 
bin.   tree(8000) 50[ms], 0.00625[ms/element] 
hash table(16000)1422[ms], 0.088875[ms/element] 
bin.   tree(16000) 120[ms], 0.0075[ms/element] 
hash table(32000)5658[ms], 0.176813[ms/element] 
bin.   tree(32000) 241[ms], 0.00753125[ms/element] 
 

Á¤¸» '½Ç¸Á'Çß½À´Ï´Ù. ÀüÇô ºü¸£Áö´Â ¾Ê½À´Ï´Ù. ¿ä¼Ò¿¡ ºñ·ÊÇØ ó¸® ½Ã°£À» ¼ÒºñÇϰí ÀÖ½À´Ï´Ù.

1.1.2 STLport

½ÇÇà °á°ú  
 
hash table(1000)0[ms], 0[ms/element] 
bin.   tree(1000) 10[ms], 0.01[ms/element] 
hash table(2000)0[ms], 0[ms/element] 
bin.   tree(2000) 0[ms], 0[ms/element] 
hash table(4000)0[ms], 0[ms/element] 
bin.   tree(4000) 20[ms], 0.005[ms/element] 
hash table(8000)20[ms], 0.0025[ms/element] 
bin.   tree(8000) 30[ms], 0.00375[ms/element] 
hash table(16000)20[ms], 0.00125[ms/element] 
bin.   tree(16000) 80[ms], 0.005[ms/element] 
hash table(32000)50[ms], 0.0015625[ms/element] 
bin.   tree(32000) 171[ms], 0.00534375[ms/element] 
 

À̰ÍÀº ¿ª½Ã ºü¸£´Ù°í ¾Ë·ÁÁ®Àִ´ë·Î 2ÁøÆ®¸®¿¡ ºñÇØ °ÅÀÇ 5¹èÀÇ ½ºÇǵ带 ³»°í ÀÖ½À´Ï´Ù.

1.1.3 SourcePro/Core

½ÇÇà °á°ú  
 
hash table(1000)11[ms], 0.011[ms/element] 
bin.   tree(1000) 10[ms], 0.01[ms/element] 
hash table(2000)10[ms], 0.005[ms/element] 
bin.   tree(2000) 10[ms], 0.005[ms/element] 
hash table(4000)20[ms], 0.005[ms/element] 
bin.   tree(4000) 20[ms], 0.005[ms/element] 
hash table(8000)60[ms], 0.0075[ms/element] 
bin.   tree(8000) 40[ms], 0.005[ms/element] 
hash table(16000)160[ms], 0.01[ms/element] 
bin.   tree(16000) 90[ms], 0.005625[ms/element] 
hash table(32000)411[ms], 0.0128437[ms/element] 
bin.   tree(32000) 190[ms], 0.0059375[ms/element] 
 

DinkumÁ¤µµ´Â ¾Æ´ÏÁö¸¸ 2ÁøÆ®¸®º¸´Ù ´ÊÀº °á°ú°¡ ³ª¿Ô½À´Ï´Ù.

ÇØ½Ã´Â Á¤¸»·Î ºü¸¥ °ÍÀÎÁö? ¿Ö ÀÌ·¯ÇÑ °á°ú°¡ µÇ¾î ¹ö·ÈÀ»±î¿ä?

ÇØ½Ã¡¤ÄÁÅ×À̳ʴ 2ÁøÆ®¸®¿¡ ºñÇØ º¹ÀâÇÕ´Ï´Ù. ÇØ½Ã°ª¸¦ ¿ä±¸ÇØ ±× °ª¿¡ ´ëÀÀÇÏ´Â ½½·Ô¿¡ ¿ä¼Ò¸¦ »ðÀÔÇÕ´Ï´Ù. Çʿ信 µû¶ó¼­ Àü¿ä¼ÒÀÇ Àç¹èÄ¡¸¦ ½Ç½ÃÇÏÁö ¾ÊÀ¸¸é ¾È µÇ´Â °Íµµ ÀÖ°ÚÁö¿ä. °Å±â¿¡ ºñ±³ÇÏ¸é ¿ä¼ÒÀÇ ºñ±³¸¦ ¹Ýº¹ÇÏ´Â 2ÁøÆ®¸®°¡ ºü¸£´Ù°í ÇÏ´Â °á°ú·Î ºñ±³¿¡ Å« ½Ã°£À» ÇÊ¿ä·Î ÇÏ´Â ¿ä¼Ò¿¡¼­´Â ½ºÇǵ尡 ¿ªÀüÇÒÁöµµ ¸ð¸¨´Ï´Ù.

´Ù½Ã ÇØ º¾½Ã´Ù. À̹ø¿¡´Â ¿ä¼Ò¸¦ ±æÀ̸¦ 8ÀÇ string·Î º¯°æÇÕ´Ï´Ù:

1.2 string


 
#include <windows.h> // GetTickCount 
#include <iostream>  // cout 
#include <set>       // set 
#include <vector>    // vector 
#include <string>    // string 
 
#if   defined(BENCH_DINKUM) 
#include <hash_set> 
 
class str_hash { 
public: 
  enum {bucket_size = 4, 
        min_buckets = 8}; 
  size_t operator()(const std::string& x) const { 
    size_t result = 0; 
    for ( int i = 0; i < x. size(); ++i ) 
      result = result * 5 + x[i]; 
    return result; 
  } 
  bool operator()(const std::string& x, const std::string& y) const 
   { return x < y; } 
}; 
 
typedef std::hash_set<std::string, str_hash> hashset_type; 
 
#elif defined(BENCH_STLPORT) 
#include <hash_set> 
 
typedef std::hash_set<std::string> hashset_type; 
 
#elif defined(BENCH_SOURCEPRO) 
#include <rw/stdex/hashset.h> 
struct strhash { 
  unsigned long operator()(const std::string& x) const {  
    unsigned long result = 0; 
    for ( int i = 0; i < x. size(); ++i ) 
      result = result * 5 + x[i]; 
    return result; 
  } 
}; 
 
typedef rw_hashset<std::string,strhash,std::equal_to<std::string>, 
                   std::allocator<std::string> > hashset_type; 
 
#endif 
 
template<class C> 
void bench(const char* msg, C& c, size_t N) { 
  std::cout << msg << '(' << N << ')'; 
  int i; 
  std::vector<std::string> data; 
  for ( i = 0; i < N; ++i ) { 
    char str_val[6]; 
    sprintf(str_val,"%08d",i); 
    data. push_back(str_val); 
  } 
  long tick = GetTickCount(); 
  for ( i = 0; i < N; ++i ) 
    c. insert(data[i]); 
  for ( i = 0; i < N; ++i ) 
    c. erase(data[i]); 
  tick = GetTickCount() - tick; 
  std::cout << tick << "[ms], " << (double)tick/(double)N << "[ms/element]" << std::endl; 
} 
 
int main() { 
  for ( size_t n = 1000; n < 40000; n += n ) { 
    hashset_type hs; 
    bench("hash table", hs, n); 
    std::set<std::string> s; 
    bench("bin.   tree", s, n); 
  } 
  return 0; 
} 
 

±×·±µ¥, °á°ú´Â...

1.2.1 Dinkum


hash table(1000) 10[ms], 0.01[ms/element] 
bin.   tree(1000) 10[ms], 0.01[ms/element] 
hash table(2000) 40[ms], 0.02[ms/element] 
bin.   tree(2000) 20[ms], 0.01[ms/element] 
hash table(4000) 161[ms], 0.04025[ms/element] 
bin.   tree(4000) 50[ms], 0.0125[ms/element] 
hash table(8000) 370[ms], 0.04625[ms/element] 
bin.   tree(8000) 111[ms], 0.013875[ms/element] 
hash table(16000) 1902[ms], 0.118875[ms/element] 
bin.   tree(16000) 230[ms], 0.014375[ms/element] 
hash table(32000) 2714[ms], 0.0848125[ms/element] 
bin.   tree(32000) 490[ms], 0.0153125[ms/element] 
 

1.2.2 STLport


hash table(1000) 0[ms], 0[ms/element] 
bin.   tree(1000) 10[ms], 0.01[ms/element] 
hash table(2000) 0[ms], 0[ms/element] 
bin.   tree(2000) 20[ms], 0.01[ms/element] 
hash table(4000) 10[ms], 0.0025[ms/element] 
bin.   tree(4000) 50[ms], 0.0125[ms/element] 
hash table(8000) 31[ms], 0.003875[ms/element] 
bin.   tree(8000) 100[ms], 0.0125[ms/element] 
hash table(16000) 80[ms], 0.005[ms/element] 
bin.   tree(16000) 220[ms], 0.01375[ms/element] 
hash table(32000) 171[ms], 0.00534375[ms/element] 
bin.   tree(32000) 481[ms], 0.0150312[ms/element] 
 

1.2.3 SourcePro/Core


hash table(1000) 10[ms], 0.01[ms/element] 
bin.   tree(1000) 10[ms], 0.01[ms/element] 
hash table(2000) 20[ms], 0.01[ms/element] 
bin.   tree(2000) 30[ms], 0.015[ms/element] 
hash table(4000) 50[ms], 0.0125[ms/element] 
bin.   tree(4000) 60[ms], 0.015[ms/element] 
hash table(8000) 100[ms], 0.0125[ms/element] 
bin.   tree(8000) 130[ms], 0.01625[ms/element] 
hash table(16000) 240[ms], 0.015[ms/element] 
bin.   tree(16000) 271[ms], 0.0169375[ms/element] 
hash table(32000) 621[ms], 0.0194063[ms/element] 
bin.   tree(32000) 571[ms], 0.0178437[ms/element] 
 

ºñ±³¿¡ ÇÊ¿ä·Î ÇÏ´Â ½Ã°£ÀÌ ±ä ¿ä¼ÒÀÇ °æ¿ì 2ÁøÆ®¸®ÀÇ Ã³¸® ¼Óµµ°¡ Å©°Ô ¶³¾îÁö´Â °ÍÀ» ¾Ë ¼ö ÀÖ°ÚÁö¿ä. ÇØ½¬ ÇÔ¼öµµ intÀÇ °æ¿ì¿Í ºñ±³Çϸé ÇØ½¬ ÇÔ¼ö ¹× µîÄ¡ ÆÇ´ÜÀÌ º¹ÀâÇÏ°Ô µÇ¾úÀ¸¹Ç·Î, ±× ¸¸Å­ ´Ê¾îÁö°í´Â ÀÖ½À´Ï´Ù¸¸, 2ÁøÆ®¸®Á¤µµÀÇ ¼Óµµ´Â º¼ ¼ö ¾ø½À´Ï´Ù.

±×·¯³ª ±×·±µ¥µµ »¡¶ó¾ß ÇÒ ÇØ½Ã°¡ 2ÁøÆ®¸®¿¡´Â ÇÊÀûÇÏÁö ¾Ê½À´Ï´Ù. ÇØ½Ã´Â ºü¸£´Ù°í ÇÏ´Â »ó½ÄÀ» µÚÁý´Â °á°ú°¡ µÇ¾ú½À´Ï´Ù.
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.