#include <chrono>
#include <iostream>
#include <memory>
struct SharedDLL{
struct Node{
Node(int i = int{})
: data(i)
, prev(nullptr)
, next(nullptr)
{}
std::shared_ptr<Node> prev, next;
int data;
};
std::shared_ptr<Node> first, last;
void push(int i){
if( first == nullptr ){
first = std::make_shared<Node>(i);
last = first;
}else{
auto temp = std::make_shared<Node>(i);
temp->prev = last;
last->next = temp;
last = temp;
}
}
int sum(){
auto ptr = first;
int result{0};
while( ptr != nullptr ){
result += ptr->data;
ptr = ptr->next;
}
return result;
}
};
struct DLL{
struct Node{
Node(int i)
: data(i)
, prev(nullptr)
, next(nullptr)
{}
Node *prev, *next;
int data;
} *first = nullptr, *last = nullptr;
void push(int i){
if( first == nullptr ){
first = new Node(i);
last = first;
}else{
Node* temp = new Node(i);
temp->prev = last;
last->next = temp;
last = temp;
}
}
int sum(){
Node* ptr = first;
int result{0};
while( ptr != nullptr ){
result += ptr->data;
ptr = ptr->next;
}
return result;
}
};
int main(){
SharedDLL l1;
DLL l2;
for( std::size_t i = 0; i < 100000UL; i++ ){
l1.push(i);
l2.push(i);
}
{
auto start = std::chrono::steady_clock::now();
long long int total;
for( std::size_t i = 0; i < 10000UL; i++ ){
total += l1.sum();
}
auto finish = std::chrono::steady_clock::now();
double elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double> >(finish - start).count();
std::cout << "l1: " << elapsed_seconds << "\n";
}
{
auto start = std::chrono::steady_clock::now();
long long int total;
for( std::size_t i = 0; i < 10000UL; i++ ){
total += l2.sum();
}
auto finish = std::chrono::steady_clock::now();
double elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double> >(finish - start).count();
std::cout << "l2: " << elapsed_seconds << "\n";
}
}