测试系统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 个stringstart = 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 个stringstart = 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 个stringstart = 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,64)==0;}
};struct Cmp2 {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;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[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 个stringstart = 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 char*到map里面start = current_usec();for(int i=0; i< count;++i) {mchar[val[i]]= rand();}end = current_usec();double mchar_insert_us = end - start;//查找count 个stringstart = 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个 map char*start = current_usec();for(int i=0; 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 个stringstart = 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;//删除count个char*start = current_usec();for(int i=0; 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 0;
}

并发为1

并发1000

并发10000

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

转载于:https://www.cnblogs.com/nancymake/p/6118982.html

C++ unordered_map 在key为string类型和char*类型时测试时间性能差异相关推荐

  1. (Redis_学习一)Redis关于string类型和hash类型数据操作

    Redis关于string类型和hash类型数据操作 set name xiaohongyang // get name setnx name xiaohy get name setex name 1 ...

  2. 数据库中的字段varchar类型和char类型的区别?

    数据库中的字段varchar类型和char类型的区别? 目录 数据库中的字段varchar类型和char类型的区别?

  3. 数据库查询字段类型为double类型和float类型时遇到的坑

    对于小数型的字段,我们常用double类型和float类型,但是这两种类型使用的时候有很大差别,下面我们来看一下 在设计数据表时,突然发现原来FLOAT原来是很不靠谱的,所以在这里建议大家换成DOUB ...

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

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

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

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

  6. byte类型和char类型

    byte字节类型是JAVA中最小的数据类型,它在内存中占8位,取值范围从-128到127, 赋值:byte i = 127; 注:byte型在赋值时,一旦超过127或小于-128,则会产生编译错误. ...

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

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

  8. 06-void类型和never类型

    void类型和never类型都是ts新增的类型,这两者的共同点是都常见用于声明函数的返回值的类型,这里我们把它们两放在一起介绍有助于大家区分. void,表示函数返回值空,即undefined. ne ...

  9. (一) 常见异常的捕获 // // (1) 编写一个程序,分别生成ArrayIndexOutOfBoundsException类型和ArithmeticException类型的异常。

    这道题不交,wdnmd public class Test6_1_zxj {// (一) 常见异常的捕获 // // (1) 编写一个程序,分别生成ArrayIndexOutOfBoundsExcep ...

  10. DATETIME类型和BIGINT 类型互相转换

    项目中使用BIGINT来存放时间,以下代码用来转换时间类型和BIGINT类型 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========= ...

最新文章

  1. 从AK47到乌兹,这款控制器模块可以模拟不同物体体感
  2. 制作一本《First Love, Last Rites》之二
  3. ThreadLocal应用-使用ThreadLocal做线程缓存变量池
  4. Machine Learning-模型评估与调参(完整版)
  5. 有哪些简单粗暴的logo设计方法?
  6. [Java基础]SimpleDateFormat类基础
  7. python数据分析方法和命令_《利用Python进行数据分析》 —— (1)
  8. 用深度学习解决Bongard问题
  9. golang mysql大量连接_golang mysql 如何设置最大连接数和最大空闲连接数
  10. Web方式预览Office/Word/Excel/pdf文件解决方案
  11. 测试自动化普遍存在的问题
  12. 韩信点兵 详解(C++)
  13. 双因素认证(two-factor authentication)
  14. Revel组件化开发框架
  15. 破解navicat时出现No All Pattern Found File Already Patched
  16. KOC十问:品牌缺钱的谎言,还是新瓶装旧酒?
  17. html 向上滚动 不间断,向上不间断滚动div+css+js模板
  18. AutoConfig工具使用指南
  19. 全局刷新和局部刷新的理解
  20. python归一化后全部都是0咋办_python归一化处理

热门文章

  1. [AHOI2006]Editor文本编辑器Splay Pascal
  2. 【概率论基础】机器学习领域必知必会的12种概率分布(附Python代码实现)
  3. 干货 | 找工作的经验总结(一)
  4. 【分类汇总】110 天以来的题解分类汇总
  5. 指令重排序导致的可见性问题
  6. 《Objective-C 程序设计(第4版) 》图书信息
  7. PyTorch:距离度量
  8. POJ读书笔记2.1 —— 鸡兔同笼
  9. NLP中GLUE数据集下载
  10. python json dump_为什么json.dump()没有以\ n结尾? - python