免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4198 | 回复: 3
打印 上一主题 下一主题

[C++] 发现forward_list不比list插入更快啊. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-06 16:59 |只看该作者 |倒序浏览
10可用积分
C++11的forward_list在cppreference上面是这样说的:

Forward list is a container which supports fast insertion and removal of elements from anywhere from the container. Fast random access is not supported. It is implemented as singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space efficient storage, when bidirectional iteration is not needed.

    号称插入和删除更快。但是我在windows上面的VC10测试了一下release版,发现不是这么回事。我用windows的高精计时器跑了一下,list的插入操作用了6106ms,forward_list用了6199ms。运行了很多次forward_list都多用了1.5%的时间。这是为什么呢,难道我的代码本身有问题?

  1. #include "stdafx.h"
  2. #include<Windows.h>
  3. #include<forward_list>
  4. #include<list>
  5. using namespace std;
  6. LARGE_INTEGER Frequency,PerformanceCount1,PerformanceCount2;
  7. void fTime1(){
  8.     QueryPerformanceCounter(&PerformanceCount1);
  9. }
  10. void fTime2(){   
  11.     QueryPerformanceCounter(&PerformanceCount2);
  12.     int tdiff=static_cast<int>((
  13.         ((PerformanceCount2.QuadPart - PerformanceCount1.QuadPart) * 1000)/Frequency.QuadPart));
  14.     printf("%d\n",tdiff);
  15. }
  16. int main(void){
  17.     const size_t nLoop=20000000;
  18.     QueryPerformanceFrequency(&Frequency);
  19.     {
  20.         fTime1();
  21.         list<int> li;
  22.         for(int i=0;i<nLoop;++i){
  23.             li.push_front(i);
  24.         }
  25.         auto it=li.begin();
  26.         ++it;
  27.         for(int i=0;i<nLoop;++i){
  28.             li.insert(it,i);//相当于insert_after begin();
  29.         }
  30.         fTime2();
  31.     }
  32.     {
  33.         fTime1();
  34.         forward_list<int> fi;
  35.         for(int i=0;i<nLoop;++i){
  36.             fi.push_front(i);
  37.         }
  38.         for(int i=0;i<nLoop;++i){
  39.             fi.insert_after(fi.begin(),i);
  40.         }
  41.         fTime2();
  42.     }
  43.     return 0;
  44. }
复制代码

最佳答案

查看完整内容

“号称插入和删除更快” ------ 没看出洋文原文中有这一句话俺洋文暴烂,只有高考时及格一次(150分的卷子正好90分),试着翻译一下:Forward list is a container which supports fast insertion and removal of elements from anywhere from the container. Fast random access is not supported. It is implemented as singly-linked list and essentially does not have any overhead compared to its implementation in C. Co ...

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2012-08-06 16:59 |只看该作者
“号称插入和删除更快” ------ 没看出洋文原文中有这一句话

俺洋文暴烂,只有高考时及格一次(150分的卷子正好90分),试着翻译一下:
Forward list is a container which supports fast insertion and removal of elements from anywhere from the container. Fast random access is not supported. It is implemented as singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space efficient storage, when bidirectional iteration is not needed.
前向列表是一个能够在任意位置快速插入和删除的容器(列表都这特性,前向列表当然也具有这特性),但不支持快速随机存取。
它是用单向链表实现的,相比较于它的C实现而言没有什么额外开销。相较于std::list而言,此容器耗费的空间更少,因为它是单向的,不是双向的。

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
3 [报告]
发表于 2012-08-06 18:12 |只看该作者
比较快慢,起码代码应该是一样的吧

auto it=li.begin();
++it;
for(int i=0;i<nLoop;++i){
    li.insert(it,i);
}

另一个是

for(int i=0;i<nLoop;++i){
    fi.insert_after(fi.begin(),i);
}

有这么比较的吗?

论坛徽章:
0
4 [报告]
发表于 2012-08-07 08:22 |只看该作者
没说有快吧。主要是省空间(节点指针;另外至少C++11要求list::size()是O(1)的,而forward_list不用)。

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP