- 论坛徽章:
- 0
|
回复 2# hgrany
谢谢。我也觉得不是红黑树实现的问题,我还有一个测试程序,使用字符串作为key,性能也是差距巨大,最少十倍,开始怀疑是strcmp两个平台性能差距大,经过测试HP上要比linux还稍微快点。
#include <map>
#include <vector>
#include <list>
#include <stdio.h>
#include <string>
#include <string.h>
#include <sys/time.h>
#include <stdio.h>
static struct timeval tm_begin, tm_end;
#define BEGIN_CALC_TIME gettimeofday (&tm_begin, NULL)
#define END_CALC_TIME gettimeofday (&tm_end, NULL)
#define SHOW_TIME \
do {\
printf ("time: %lu\n", 1000000 * (tm_end.tv_sec - tm_begin.tv_sec) + (tm_end.tv_usec - tm_begin.tv_usec)); \
} while (0)
struct IdxClient_key
{
char ClientId[16];
bool operator < (const IdxClient_key& s) const
{
int ret;
ret = strcmp (ClientId, s.ClientId);
if (ret < 0) {
return true;
}
return false;
}
};
class ST_Client
{
public:
ST_Client ()
{
memset (this, 0, sizeof (ST_Client));
}
char ClientId[16];
char OpenDate[8];
};
int
main (int argc, char *argv[])
{
int i = 0;
std::map<IdxClient_key, ST_Client *> mapA;
std::vector<IdxClient_key> vecA;
IdxClient_key key;
vecA.reserve (1000000);
for (i = 0; i < 1000000; ++i) {
ST_Client *p = new ST_Client;
snprintf (p->ClientId, 16, "cid%d", i);
/// snprintf (p->MemberId, 9, "%d", i);
snprintf (p->OpenDate, 8, "20120508");
memcpy (key.ClientId, p->ClientId, 16);
mapA.insert (std::make_pair (key, p));
vecA.push_back (key);
}
printf ("table size: %d\n", mapA.size ());
std::vector<IdxClient_key>::iterator iter = vecA.begin ();
std::vector<IdxClient_key>::iterator iter_end = vecA.end ();
BEGIN_CALC_TIME;
#if 1
for (; iter != iter_end; ++iter) {
std::map<IdxClient_key, ST_Client *>::iterator ret = mapA.find (*iter);
if (ret == mapA.end ()) {
printf ("find error.\n");
return -1;
}
mapA.erase (ret);
}
#endif
END_CALC_TIME;
SHOW_TIME;
printf ("table size: %d\n", mapA.size ());
}
|
|