gpt4 book ai didi

c++ - STL 列表的 push_back 性能不佳?

转载 作者:行者123 更新时间:2023-11-30 03:11:05 25 4
gpt4 key购买 nike

我编写了一个简单的程序来针对简单的 C 类列表数据结构测试 STL 列表性能。它在“push_back()”行表现不佳。对此有何评论?

$ ./test2
Build the type list : time consumed -> 0.311465
Iterate over all items: time consumed -> 0.00898
Build the simple C List: time consumed -> 0.020275
Iterate over all items: time consumed -> 0.008755

源代码是:

#include <stdexcept>
#include "high_resolution_timer.hpp"

#include <list>
#include <algorithm>
#include <iostream>


#define TESTNUM 1000000

/* The test struct */
struct MyType {
int num;
};


/*
* C++ STL::list Test
*/
typedef struct MyType* mytype_t;

void myfunction(MyType t) {
}

int test_stl_list()
{
std::list<mytype_t> mylist;
util::high_resolution_timer t;

/*
* Build the type list
*/
t.restart();
for(int i = 0; i < TESTNUM; i++) {
mytype_t aItem;
aItem->num = i;
mylist.push_back(aItem);
}
std::cout << " Build the type list : time consumed -> " << t.elapsed() << std::endl;


/*
* Iterate over all item
*/
t.restart();
std::for_each(mylist.begin(), mylist.end(), myfunction);
std::cout << " Iterate over all items: time consumed -> " << t.elapsed() << std::endl;

return 0;
}

/*
* a simple C list
*/
struct MyCList;
struct MyCList{
struct MyType m;
struct MyCList* p_next;
};

int test_simple_c_list()
{
struct MyCList* p_list_head = NULL;
util::high_resolution_timer t;

/*
* Build it
*/
t.restart();
struct MyCList* p_new_item = NULL;
for(int i = 0; i < TESTNUM; i++) {
p_new_item = (struct MyCList*) malloc(sizeof(struct MyCList));
if(p_new_item == NULL) {
printf("ERROR : while malloc\n");
return -1;
}
p_new_item->m.num = i;
p_new_item->p_next = p_list_head;
p_list_head = p_new_item;
}
std::cout << " Build the simple C List: time consumed -> " << t.elapsed() << std::endl;

/*
* Iterate all items
*/
t.restart();
p_new_item = p_list_head;
while(p_new_item->p_next != NULL) {
p_new_item = p_new_item->p_next;
}
std::cout << " Iterate over all items: time consumed -> " << t.elapsed() << std::endl;


return 0;
}

int main(int argc, char** argv)
{
if(test_stl_list() != 0) {
printf("ERROR: error at testcase1\n");
return -1;
}

if(test_simple_c_list() != 0) {
printf("ERROR: error at testcase2\n");
return -1;
}

return 0;
}

糟糕,是的。我修改了代码,它显示:

$ ./test2
Build the type list : time consumed -> 0.163724
Iterate over all items: time consumed -> 0.005427
Build the simple C List: time consumed -> 0.018797
Iterate over all items: time consumed -> 0.004778

那么,我的问题是,为什么我的“push_back”代码性能不佳?

最佳答案

有一件事是,在 C 中,您有一个对象链表,但在 C++ 中,您有一个指针链表(因此一方面,您进行了两倍的分配)。要同类比较,您的 STL 代码应该是:

int test_stl_list()
{
std::list<MyType> mylist;
util::high_resolution_timer t;

/*
* Build the type list
*/
t.restart();
for(int i = 0; i < TESTNUM; i++) {
MyItem aItem;
aItem.num = i;
mylist.push_back(aItem);
}
std::cout << " Build the type list : time consumed -> " << t.elapsed() << std::endl;


return 0;
}

关于c++ - STL 列表的 push_back 性能不佳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2588962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com