/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: bsl_unsafe_hash.cpp,v 1.2 2008/11/12 04:00:31 xiaowei Exp $ * **************************************************************************/ /** * @file bsl_unsf_hash.h * @author xiaowei(com@baidu.com) * @date 2008/08/22 18:53:11 * @version $Revision: 1.2 $ * @brief * **/ #include #include #include #include #include #include #include #include #include #include #include #include "stl_hashmap.h" /* * 单线程下的性能测试比较 */ struct point_t { size_t hash_bucket; size_t hash_datanum; int thread_num; int value_size; long query_loop; std::string hash_type; public: point_t() { hash_bucket = 0; hash_datanum = 0; thread_num = 0; value_size = 0; query_loop = 0; hash_type = ""; } }; template struct type_t { char data[size]; }; template void test_insert(hash_t &hash, int datanum) { Slotime st(1); st.start(); for (int i=0; i= g_num) g_pos = 0; int *val = g_randsize[g_pos]; ++ g_pos; pthread_mutex_unlock(&g_lock); return val; } template struct test_query_t { hash_t & _1; Slotime _2; point_t & _3; public: test_query_t(hash_t & h, point_t &p) : _1(h), _2(p.thread_num), _3(p) {} }; template void * test_query_r(void *arg) { test_query_t *fd = (test_query_t *)arg; type_t v; int *val = getrand(); long loop = fd->_3.query_loop / (long)fd->_3.thread_num; fd->_2.start(); for (long i=0; i_1.get(val[i%g_size], &v); } fd->_2.stop(); return NULL; } template void test_query(hash_t &hash, point_t &p) { test_query_t val(hash, p); run_thread(test_query_r, &val, p.thread_num); //std::cout<<" "< void test(hash_t &hash, point_t &p) { test_insert(hash, p.hash_datanum); test_query(hash, p); } template void testcase(point_t &p) { //stl::hashmap { g_pos = 0; std::cout<<"stl_hashmap "< > xhash_t; xhash_t hash; p.hash_type = "stl_hashmap"; test >(hash, p); std::cout< > anshash1_t; anshash1_t hash; hash.create(p.hash_bucket, ans_hash_fcnt); p.hash_type = "ans_hashmap"; test >(hash, p); std::cout< > rm_t; rm_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_readmap"; std::cout< > >vec; for (int i=0; i<(int)p.hash_datanum; ++i) { vec.push_back(std::make_pair(i, type_t())); } hash.assign(vec.begin(), vec.end()); test_query >(hash, p); hash.clear(); } //bsl::hashmap { g_pos = 0; std::cout<<"bsl_hashmap sample_alloc "< > bslhash1_t; bslhash1_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_hashmap/sample_alloc"; test >(hash, p); std::cout<, bsl::xhash, bsl::equal, bsl::bsl_alloc > bslhash2_t; bslhash2_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_hashmap/alloc"; test >(hash, p); std::cout<, bsl::xhash, bsl::equal, bsl::bsl_cpsalloc > > bslhash3_t; bslhash3_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_hashmap/cpsalloc"; test >(hash, p); std::cout< > bslphash1_t; bslphash1_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_phashmap/sample_alloc"; test >(hash, p); std::cout<, bsl::xhash, bsl::equal, bsl::bsl_alloc > bslphash2_t; bslphash2_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_phashmap/alloc"; test >(hash, p); std::cout<, bsl::xhash, bsl::equal, bsl::bsl_cpsalloc > > bslphash3_t; bslphash3_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_phashmap/cpsalloc"; test >(hash, p); std::cout<("hash_bucket"); p.hash_datanum = pb::getopt("hash_datanum"); p.thread_num = pb::getopt("thread_num"); p.value_size = pb::getopt("value_size"); p.query_loop = pb::getopt("query_loop"); p.query_loop *= 1000000; #if 0 pb::put_result("hash_bucket", (int)p.hash_bucket); pb::put_result("hash_datanum", (int)p.hash_datanum); pb::put_result("thread_num", (int)p.thread_num); pb::put_result("value_size", (int)p.value_size); pb::put_result("query_loop", (int)p.query_loop); #endif //testcase(p); if (p.value_size <= 4) { testcase (p); } else if (p.value_size <= 8) { testcase (p); } else if (p.value_size < 32) { testcase (p); } else { testcase (p); } return 0; } /* vim: set ts=4 sw=4 sts=4 tw=100 noet: */