همون طوری که -7 گفت list لیست پیوندی 2 طرفست ولی forward_list یک طرفه
از نظر سرعت forward_list یک مقدار خیلی جزیی زمان پر شدن سریع تره
ولی از نظر حجم مصرفی نمیشه گفت همیشه forward_list فضای کمتری رو اشغال می کنه .
توی تست هایی که انجام دادم برای 50 میلیون عنصر توی سیستم من نتیجه به این شکل شد :
//long long
// size(mb) time
//forward list 1157.3 2360
//list 1157.3 2450
//char
// size(mb) time
//forward list 773 2185
//list 1157 2400
همون جوری که مشخصه از نظر سرعت تفاوتشون خیلی خیلی کمه .
از نظر فضای مصرفی هم بستگی داره داخلشون چی ریخته باشین
دلیلی که توی کد بالا فضای مصرفی با وجود عوض کردن char با long long فرقی نکرده :
-
forward_list<char>
node:
sizeof(void*) + sizeof(char) == 5
, which the memory allocator will round up to an 8 byte allocation.
-
forward_list<long long>
node:
sizeof(void*) + sizeof(long long) == 12
, which the memory allocator will round up to an 16 byte allocation.
-
list<char>
node:
2 * sizeof(void*) + sizeof(char) == 9
, which the memory allocator will round up to an 16 byte allocation.
-
list<long long>
node:
2 * sizeof(void*) + sizeof(long long) == 16
, which the memory allocator won't round since 16 is already a multiple of 8
یعنی این که سایز همیشه کمتره درست نیست خیلی وقت ها سایز مورد استفاده هم دقیقا برابره .
کد تست :
#include <iostream>
#include <list>
#include <forward_list>
#include <windows.h>
int main()
{
LARGE_INTEGER start_;
LARGE_INTEGER end_;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
std::list<long long> list;
QueryPerformanceCounter(&start_);
for(long long i=0;i<50000000;i++)
list.push_front(i);
QueryPerformanceCounter(&end_);
std::cout<< (end_.QuadPart - start_.QuadPart) / (freq.QuadPart / 1000) <<"\n";
std::cin.get();
}