感觉不是很严谨的测试下,看看C++的STL有多慢

首先这是一个链表的测试,

一个是我自己纯C写的链表,

一个是用的C++的STL的list,

节点都是一个结构体,结构体包含int,char*指针等等,

 

测试执行的过程是 给定节点数,然后就开始创建链表,创建节点,创建节点的过程中有内存分配,

然后在链表中加入节点,

随机查询节点,次数为链表节点总数的两倍,

随机查询并将节点从链表中删除,次数为链表节点总数的三分之一,

最后释放整个链表

我的 测试环境是虚拟机,配置低下,2核心256M的内存

执行效果如下

1000个节点的情况

[root@CentOS7 wonrowl]# ./wonrowllist_c 1000
start create list .....
 create list over with failed 0 ,   use time 39ms 
 random read NOT DEL 2000  with failed 0 ,   use time 12ms 
 random read ADD DEL FROM LIST  333  with failed 0 ,   use time 1ms 
 Left nod in list 667  has been cleared 

这是纯C的链表

[root@CentOS7 cppstl]# ./stl 1000
start create stl list .....
 create stl list over with failed 0 ,   use time 45ms 
 random read STL  NOT DEL 2000  with failed 0 ,   use time 15ms 
 random read ADD DEL FROM LIST by STL  333  with failed 0 ,   use time 1ms 
 left 667 in stl list has been cleared 

这是stl的

感觉差别不大

 

然后增加 节点数到5000

 

[root@CentOS7 wonrowl]# ./wonrowllist_c 5000
start create list .....
 create list over with failed 0 ,   use time 2742ms 
 random read NOT DEL 10000  with failed 0 ,   use time 7013ms 
 random read ADD DEL FROM LIST  1666  with failed 0 ,   use time 76ms 
 Left nod in list 3334  has been cleared 
-------------------------------------------------------------------------------------------------

[root@CentOS7 cppstl]# ./stl 5000
start create stl list .....
 create stl list over with failed 0 ,   use time 2124ms 
 random read STL  NOT DEL 10000  with failed 0 ,   use time 8356ms 
 random read ADD DEL FROM LIST by STL  1666  with failed 0 ,   use time 313ms 
 left 3334 in stl list has been cleared 

差距明显出来了,

stl在创建的时候比较快,应该是得益于c++的new的效率高于c的malloc,反正文档是这么写的

 

最后尝试20K的节点数

[root@CentOS7 wonrowl]# ./wonrowllist_c 20000
start create list .....
 create list over with failed 0 ,   use time 40366ms 
 random read NOT DEL 40000  with failed 0 ,   use time 56761ms 
 random read ADD DEL FROM LIST  6666  with failed 0 ,   use time 785ms 
 Left nod in list 13334  has been cleared 
------------------------------------------------------------------------------------------

[root@CentOS7 cppstl]# ./stl 20000
start create stl list .....
 create stl list over with failed 0 ,   use time 34855ms 
 random read STL  NOT DEL 40000  with failed 0 ,   use time 89332ms 
 random read ADD DEL FROM LIST by STL  6666  with failed 0 ,   use time 2761ms 
 left 13334 in stl list has been cleared 

 

果然除了创建的时候快点,其他的查询删除各方面,stl全面落败,

 

所以以后写服务器想要效率的骚年,还是花一两天时间自己创建一个适合的链表吧,

 


typedef struct general_wonrowl_node
{     
    int  node_id;
    char *data_ptr;               
    int  data_len;                
    int  data_type;              
    int  data_from;             
    int  data_to;                 
    ///请求数据关系映射链表所用的数据结构

    int  requestor_of_client;      
    int  wanted_dev;                
    int  requstor_index;           
     int  requestor_data_type;      

   struct list_head list;       
}General_Wonrowl_Node;


extern Wonrowl_Root_List* Create_Wonrowl_Root_List();
extern struct list_head * Create_Head();  
extern General_Wonrowl_Node *Alloc_General_Wonrowl_Node(int buffer_size);
extern void Add_Node_At_Head(Wonrowl_Root_List *root,General_Wonrowl_Node *new_node);
extern void Add_Node_At_Tail(Wonrowl_Root_List *root,General_Wonrowl_Node *new_node);
extern General_Wonrowl_Node * Get_Node_At_Head(Wonrowl_Root_List *root);
extern General_Wonrowl_Node * Get_Node_At_Tail(Wonrowl_Root_List *root);
extern General_Wonrowl_Node * Get_Specific_Node(Wonrowl_Root_List *root,int wanted_id);
extern General_Wonrowl_Node * Get_Specific_Node_Not_Del(Wonrowl_Root_List *root,int wanted_id);
extern int Count_List(Wonrowl_Root_List * root);
extern void Destroy_General_Wonrowl_Node(General_Wonrowl_Node **nd);
extern int Clear_List(Wonrowl_Root_List * root); 

这是数据结构

[root@CentOS7 wonrowl]# valgrind --leak-check=full --show-reachable=yes --trace-children=yes ./wonrowllist_c 500
==2893== Memcheck, a memory error detector
==2893== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2893== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2893== Command: ./wonrowllist_c 500
==2893== 
start create list .....
 create list over with failed 0 ,   use time 70ms 
 random read NOT DEL 1000  with failed 0 ,   use time 9ms 
 random read ADD DEL FROM LIST  166  with failed 0 ,   use time 5ms 
 Left nod in list 334  has been cleared 
==2893== 
==2893== HEAP SUMMARY:
==2893==     in use at exit: 104 bytes in 2 blocks
==2893==   total heap usage: 1,002 allocs, 1,000 frees, 51,232,604 bytes allocated
==2893== 
==2893== 16 bytes in 1 blocks are still reachable in loss record 1 of 2
==2893==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==2893==    by 0x400B11: Create_Head (in /mnt/hgfs/linuxshare/others/wonrowllist_vs_stl/wonrowl/wonrowllist_c)
==2893==    by 0x400B6E: Create_Wonrowl_Root_List (in /mnt/hgfs/linuxshare/others/wonrowllist_vs_stl/wonrowl/wonrowllist_c)
==2893==    by 0x4014D5: main (in /mnt/hgfs/linuxshare/others/wonrowllist_vs_stl/wonrowl/wonrowllist_c)
==2893== 
==2893== 88 bytes in 1 blocks are still reachable in loss record 2 of 2
==2893==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==2893==    by 0x400B52: Create_Wonrowl_Root_List (in /mnt/hgfs/linuxshare/others/wonrowllist_vs_stl/wonrowl/wonrowllist_c)
==2893==    by 0x4014D5: main (in /mnt/hgfs/linuxshare/others/wonrowllist_vs_stl/wonrowl/wonrowllist_c)
==2893== 
==2893== LEAK SUMMARY:
==2893==    definitely lost: 0 bytes in 0 blocks
==2893==    indirectly lost: 0 bytes in 0 blocks
==2893==      possibly lost: 0 bytes in 0 blocks
==2893==    still reachable: 104 bytes in 2 blocks
==2893==         suppressed: 0 bytes in 0 blocks
==2893== 
==2893== For lists of detected and suppressed errors, rerun with: -s
==2893== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
 

 

最后附上 valgrind的内存检查,内存很干净,所有malloc的空间也全部释放, 

不管是中途取出的节点,还是最后释放链表,

still reachable: 104 bytes in 2 blocks 是我调试的时候申请的,不是链表的操作

同时接口也加入了锁机制,所以是线程安全的,至于为什么做这个测试,

主要是有个傻叉非要将让我将现在的纯C写的流媒体服务器(该服务器做音视频转发和数据接入,没有任何问题,而且效率很高)用c++写一次,理由是opencv都早用c++了,草,

然后跟我扯什么面向对象,stl的好处,我去他大爷的~
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值