测试系统liunx centos6.5

代码如下

#include <string.h>
#include <sstream>
#include <list>
#include <sys/time.h>
#include <unordered_map>
#include <cstdlib>
#include <stdio.h>
#include "unistd.h" using namespace std; struct Cmp {
bool operator()(const char* a,const char* b) const {
return memcmp(a,b,64)==0;
}
}; struct hash_func {
int operator()(const char* str) const {
int seed = 131;
int hash = 0;
hash = (hash * seed) + (*str);
while(*(++str)) {
hash = (hash * seed) + (*str);
} return hash & (0x7FFFFFFF);
}
}; double current_usec(){
struct timeval tv;
gettimeofday( &tv, NULL );
return tv.tv_sec * 1000 * 1000 + tv.tv_usec;
} //产生随机串
char* genRandomString(int length){
sleep(1);
int flag, i;
char* string;
srand((unsigned) time(NULL));
if ((string = (char*) malloc(length)) == NULL ) {
printf("Malloc failed!flag:14\n");
return NULL ;
} for (i = 0; i < length - 1; i++)
{
flag = rand() % 3;
switch (flag)
{
case 0:
string[i] = 'A' + rand() % 26;
break;
case 1:
string[i] = 'a' + rand() % 26;
break;
case 2:
string[i] = '0' + rand() % 10;
break;
default:
string[i] = 'x';
break;
}
} string[length - 1] = '\0';
return string;
} int main(int argc, char* argv[] ){
char* value;
string s;
std::unordered_map<const char*, int, hash_func, Cmp> mapchar;
std::unordered_map<const char*, int, hash_func, Cmp>::iterator itchar;
std::unordered_map<std::string, int> mapstring;
std::unordered_map<std::string, int>::iterator itstring; int count = atoi(argv[1]);
if(count == 0) {
printf("the count is zero");
return 0;
} std::string str[30000];
char* val[30000];
double start=0;
double end = 0; int i=0;
int num = count;
char v[64];
while(num) {
value = genRandomString(64);
strcpy(v,value);
val[i] = value;
s = value;
str[i++] = s;
--num;
} //插入count 个string
start = current_usec();
for(int i=0; i< count;++i) {
mapstring[str[i]]= rand();
}
end = current_usec();
double string_insert_us = end - start; //插入count 个char*
start = current_usec();
for(int i=0; i< count;++i) {
mapchar[val[i]]= rand();
}
end = current_usec();
double char_insert_us = end - start; //查找count 个string
start = current_usec();
for(int i=0; i<count; ++i) {
itstring = mapstring.find(str[i]);
if( itstring == mapstring.end()) {
printf("string not find,something wrong with it");
}
}
end = current_usec();
double string_find_us = end - start; //查找count个char*
start = current_usec();
for(int i=0; i<count; ++i) {
itchar = mapchar.find(val[i]);
if( itchar == mapchar.end()) {
printf("char not find,something wrong with it");
}
}
end = current_usec();
double char_find_us = end - start; //删除count 个string
start = current_usec();
for(int i=0; i<count; ++i) {
mapstring.erase(str[i]);
}
end = current_usec();
double string_del_us = end - start; //删除count 个char*
start = current_usec();
for(int i=0; i<count; ++i) {
mapchar.erase(val[i]);
}
end = current_usec();
double char_del_us = end - start; printf("插入string time is %f us \n", string_insert_us/count);
printf("插入char time is %f us\n", char_insert_us/count);
printf("查找string time is %f us\n", string_find_us/count);
printf("查找char time is %f us\n", char_find_us/count);
printf("删除string time is %f us\n", string_del_us/count);
printf("删除char time is %f us\n", char_del_us/count); return 0;
}

  插入的字符串是64位的字符串,

在并发1个情况下

在并发10的情况下

并发1000

并发5000

并发10000

一开始我以为char* 的速度会快,因为插入string的时候是要构造string申请内存的,可能是我的hash函数比系统的要慢了,但是没想到会是这个结果,有相关经验的朋友可以看下是不是我写的代码有问题或者是什么情况导致的这个情况。

把hash函数修改成inline处理一下,并且加了一个map函数key为char*的进行对比

源码

#include <string.h>
#include <sstream>
#include <list>
#include <sys/time.h>
#include <unordered_map>
#include <cstdlib>
#include <stdio.h>
#include "unistd.h"
#include <map> using namespace std; struct Cmp {
inline bool operator()(const char* a,const char* b) const {
return memcmp(a,b,)==;
}
}; struct Cmp2 {
bool operator()(const char* a,const char* b) const {
return memcmp(a,b,)<;
}
}; struct hash_func {
int operator()(const char* str) const {
int seed = ;
int hash = ;
hash = (hash * seed) + (*str);
while(*(++str)) {
hash = (hash * seed) + (*str);
} return hash & (0x7FFFFFFF);
}
}; double current_usec(){
struct timeval tv;
gettimeofday( &tv, NULL );
return tv.tv_sec * * + tv.tv_usec;
} //产生随机串
char* genRandomString(int length){
sleep();
int flag, i;
char* string;
srand((unsigned) time(NULL));
if ((string = (char*) malloc(length)) == NULL ) {
printf("Malloc failed!flag:14\n");
return NULL ;
} for (i = ; i < length - ; i++)
{
flag = rand() % ;
switch (flag)
{
case :
string[i] = 'A' + rand() % ;
break;
case :
string[i] = 'a' + rand() % ;
break;
case :
string[i] = '' + rand() % ;
break;
default:
string[i] = 'x';
break;
}
} string[length - ] = '\0';
return string;
} int main(int argc, char* argv[] ){
char* value;
string s;
std::unordered_map<const char*, int, hash_func, Cmp> mapchar;
std::unordered_map<const char*, int, hash_func, Cmp>::iterator itchar;
map<const char*, int, Cmp2> mchar;
map<const char*, int, Cmp2>::iterator mitchar;
std::unordered_map<std::string, int> mapstring;
std::unordered_map<std::string, int>::iterator itstring; int count = atoi(argv[]);
if(count == ) {
printf("the count is zero");
return ;
} std::string str[];
char* val[];
double start=;
double end = ; int i=;
int num = count;
char v[];
while(num) {
value = genRandomString();
strcpy(v,value);
val[i] = value;
s = value;
str[i++] = s;
--num;
} //插入count 个string
start = current_usec();
for(int i=; i< count;++i) {
mapstring[str[i]]= rand();
}
end = current_usec();
double string_insert_us = end - start; //插入count 个char*
start = current_usec();
for(int i=; i< count;++i) {
mapchar[val[i]]= rand();
}
end = current_usec();
double char_insert_us = end - start; //插入count char*到map里面
start = current_usec();
for(int i=; i< count;++i) {
mchar[val[i]]= rand();
}
end = current_usec();
double mchar_insert_us = end - start; //查找count 个string
start = current_usec();
for(int i=; i<count; ++i) {
itstring = mapstring.find(str[i]);
if( itstring == mapstring.end()) {
printf("string not find,something wrong with it");
}
}
end = current_usec();
double string_find_us = end - start; //查找count个char*
start = current_usec();
for(int i=; i<count; ++i) {
itchar = mapchar.find(val[i]);
if( itchar == mapchar.end()) {
printf("char not find,something wrong with it");
}
}
end = current_usec();
double char_find_us = end - start; //查找count个 map char*
start = current_usec();
for(int i=; i<count; ++i) {
mitchar = mchar.find(val[i]);
if( mitchar == mchar.end()) {
printf("map char not find,something wrong with it");
}
}
end = current_usec();
double mchar_find_us = end - start; //删除count 个string
start = current_usec();
for(int i=; i<count; ++i) {
mapstring.erase(str[i]);
}
end = current_usec();
double string_del_us = end - start; //删除count 个char*
start = current_usec();
for(int i=; i<count; ++i) {
mapchar.erase(val[i]);
}
end = current_usec();
double char_del_us = end - start; //删除count个char*
start = current_usec();
for(int i=; i<count; ++i) {
mchar.erase(val[i]);
}
end = current_usec();
double mchar_del_us = end - start; printf("插入string time is %f us \n", string_insert_us/count);
printf("插入char time is %f us\n", char_insert_us/count);
printf("插入map char time is %f us\n", mchar_insert_us/count);
printf("查找string time is %f us\n", string_find_us/count);
printf("查找char time is %f us\n", char_find_us/count);
printf("查找map char time is %f us\n", mchar_find_us/count);
printf("删除string time is %f us\n", string_del_us/count);
printf("删除char time is %f us\n", char_del_us/count);
printf("删除map char time is %f us\n", mchar_del_us/count); return ;
}

并发为1

并发1000

并发10000

主要原因我猜我的hash函数自己定义的比较费时间了,需要再仔细的考虑一下看下如何能进一步的省去这个时间

C++ unordered_map 在key为string类型和char*类型时测试时间性能差异的更多相关文章

  1. string类型和int类型之间的转换

    一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...

  2. Redis 笔记与总结2 String 类型和 Hash 类型

    Linux 版本信息: cat /etc/issue 或cat /etc/redhat-release(Linux查看版本当前操作系统发行版信息) CentOS release 6.6 (Final) ...

  3. 02_NoSQL数据库之Redis数据库:string类型和hash类型

     Strings类型及操作 String是最简单的类型,一个key对应一个Value,String类型是二进制安全的.Redis的String可以包含任何数据,比如jpg图片或者序列化的对象. S ...

  4. 第一节: Redis之String类型和Hash类型的介绍和案例应用

    一. String类型基础 1.类型介绍 典型的Key-Value集合,如果要存实体,需要序列化成字符串,获取的时候需要反序列化一下. 2. 指令Api说明 3.常用Api说明 (1).StringS ...

  5. C# string类型和byte[]类型相互转换

    string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: ...

  6. 关于 实体类中 时间字段 为string 类型和 datatime类型 比较

    经发现, 数据库中保存时间格式数据  可以正常 排序, 数据中保存时间格式字符串 排序出现问题 /// <summary> /// 修改时间 /// </summary> pu ...

  7. int类型和char类型的区别

    下面三个定义式的区别: int i = 1; char i = 1; char i = '1'; int用来定义整型变量,char用来定义字符型变量,要清楚的知道三个定义式的区别,可以比较它们在内存中 ...

  8. AngularJs:String类型和JSON相互转换

    最近一周做了一个页面,制作的过程中遇到各种问题,从中可以看出本人的js基础还不够扎实,angularjs也只是刚入门的水平,现在将制作过程中遇到的问题一一汇总,方便以后查阅. 一.String类型和J ...

  9. Date类型和Long类型的相互转换

    Date类型和Long类型的相互转换: import java.text.SimpleDateFormat; import java.util.Date; public class T { publi ...

随机推荐

  1. .net中,控件(Name)属性或ID属性的常见命名规则

    控件名称 缩写 介绍 公共控件   Button btn 按钮 CheckBox chk 复选框 CheckedListBox ckl 显示一个项列表,其中每一项左侧都有一个复选框 ComboBox ...

  2. Delphi的注册表操作

    转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息.     一.创 ...

  3. MVC 插件式开发

    MVC 插件式开发 在开发一个OA系统是,我们可能遇到 A模块. B模块 .C模块,这也模块组成一个完整的系统,买给客服.现在又有一个客服要我们做一个OA系统,唉我们发现,跟上一个OA系统差不多,但没 ...

  4. rsync服务器

    转自:http://www.mike.org.cn/blog/index.php?load=read&id=639###pp=0 [rsync实现网站的备份,文件的同步,不同系统的文件的同步, ...

  5. javascript call和apply

    每个函数都包含两个非继承而来的方法:call和apply. 我们可以通过这两个方法来间接调用函数.可以这样: f.call(o); f.apply(o); //o对象间接调用了f函数 这与下面的功能相 ...

  6. EF4.0、4.3创建表达式树状动态查询总结

    ---------------------------------------------快速适用 效果: where name like '%王%' and Age>=35 or Age< ...

  7. Apworks到底是什么?

    Apworks到底是什么? 简介 Apworks是一款基于Microsoft .NET的面向领域驱动的企业级应用程序开发框架,它适用于以领域模型为核心的企业级系统的开发和集成.Apworks不仅能够很 ...

  8. [置顶] android调用第三方库——第四篇——调用多个第三方库

    0:前言: 在前面三篇中我们介绍了android调用第三方库的形式,在这一篇中我们介绍调用多个第三方库的Android.mk的写法,由于其他三篇介绍的很详细,这里只给出Android.mk的内容. [ ...

  9. MVC应用程序请求密码的功能1

    MVC应用程序请求密码的功能(一) 经过一系列的练习,实现了会员注册<MVC会员注册>http://www.cnblogs.com/insus/p/3439599.html,登录<M ...

  10. FineUI Grid 缓存列显示隐藏状态

    当列表字段过多时,需要隐藏掉一些,但是再次打开页面又显示出来了,FineUI没有提供缓存功能,那么自己动手,打开[ext-part2.js]找到 “if (Ext.grid.column.RowNum ...