Created
May 20, 2014 04:47
-
-
Save miguelmartin75/68009df7734f91640a78 to your computer and use it in GitHub Desktop.
response to reddit comment http://www.reddit.com/r/programming/comments/25xpre/bjarne_stroustrup_why_you_should_avoid_linked/chmedsf?context=1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <list> | |
#include <cstdlib> | |
#include <algorithm> | |
#include <functional> | |
#include <chrono> | |
int entries = 10000; | |
typedef struct node { | |
int val; | |
struct node * next; | |
} node_t; | |
void add_to_list(node_t **list, int value){ | |
node_t *tmp = *list; | |
node_t * new_node = (node_t *)malloc(sizeof(node_t)); | |
new_node->val=value; | |
new_node->next = NULL; | |
if( value <= tmp->val){ | |
new_node->next = tmp; | |
*list = new_node; | |
return; | |
} | |
while( tmp->next ){ | |
if( value <= tmp->next->val ){ | |
new_node->next = tmp->next; | |
tmp->next = new_node; | |
return; | |
} | |
tmp = tmp->next; | |
} | |
tmp->next = new_node; | |
return; | |
} | |
void remove_from_list(node_t **list, size_t element){ | |
size_t location = 0; | |
node_t *tmp = *list; | |
node_t * parent; | |
if( element == 0 ){ | |
*list = tmp->next; | |
free(tmp); | |
return; | |
} | |
while(tmp){ | |
if( location == element ){ | |
parent->next = tmp->next; | |
free(tmp); | |
break; | |
} | |
location++; | |
parent = tmp; | |
tmp = tmp->next; | |
} | |
} | |
void print_list(node_t *list){ | |
node_t *tmp = list; | |
while( tmp != NULL ){ | |
std::cout<<tmp->val<<" "; | |
tmp = tmp->next; | |
} | |
std::cout<<"\n"; | |
} | |
template<typename C> | |
void insert_ordered(C& c, typename C::value_type x) | |
{ | |
typename C::iterator i = std::find_if(c.begin(), c.end(), | |
std::bind1st(std::less<int>(),x) | |
); | |
if (i == c.end()) | |
c.push_back(x); | |
else | |
c.insert(i, x); | |
} | |
template<typename C> | |
void erase_element(C& c, size_t member) | |
{ | |
size_t i = 0; | |
typename C::iterator it = c.begin(); | |
for( size_t i = 0; i < member; i++ ){ | |
i++; | |
} | |
c.erase(it); | |
return; | |
} | |
template<typename C> | |
void dump(C& c ){ | |
for ( typename C::iterator it = c.begin(); it != c.end(); ++it){ | |
std::cout << *it << ' ' ; | |
} | |
std::cout << "\n"; | |
} | |
typedef double Seconds; | |
inline Seconds GetTimeNow() | |
{ | |
using namespace std; | |
return chrono::duration_cast<chrono::duration<Seconds, std::ratio<1> > >(chrono::high_resolution_clock::now().time_since_epoch()).count(); | |
} | |
int main (int argc, char *argv[]) | |
{ | |
int r; | |
int debug = 1; | |
if(argc == 1) | |
{ | |
std::cout << "invalid input\n"; | |
return 1; | |
} | |
if( argc > 1 ){ | |
entries = atoi(argv[1]); | |
} | |
if( argc > 2 ){ | |
debug = atoi(argv[2]); | |
} | |
time_t seed = time(NULL); | |
// constructors used in the same order as described above: | |
std::vector<int> test_vector; | |
std::list<int> test_list; | |
node_t *test_link = NULL; | |
Seconds vec_start = GetTimeNow(); // get time now | |
srand(seed); | |
for (int i = 0; i < entries; ++i){ | |
insert_ordered(test_vector, rand()); | |
if( debug == 1 ) | |
dump(test_vector); | |
} | |
for (int i = 0; i < entries; ++i){ | |
if( test_vector.size() == 1 ){ | |
erase_element(test_vector, 0 ); | |
}else{ | |
erase_element(test_vector, rand()%(test_vector.size()-1) ); | |
} | |
if( debug == 1 ) | |
dump(test_vector); | |
} | |
Seconds vec_end = GetTimeNow(); | |
Seconds list_start = GetTimeNow(); // get time now | |
srand(seed); | |
for (int i = 0; i < entries; ++i){ | |
insert_ordered(test_list, rand()); | |
if( debug == 1 ) | |
dump(test_list); | |
} | |
for (int i = 0; i < entries; ++i){ | |
if( test_list.size() == 1 ){ | |
erase_element(test_list, 0 ); | |
}else{ | |
erase_element(test_list, rand()%(test_list.size()-1) ); | |
} | |
if( debug == 1 ) | |
dump(test_list); | |
} | |
Seconds list_end = GetTimeNow(); | |
Seconds link_start = GetTimeNow(); // get time now | |
srand(seed); | |
test_link = (node_t *)malloc(sizeof(node_t)); | |
test_link->val = rand(); | |
test_link->next = NULL; | |
if( debug == 1 ) | |
print_list(test_link); | |
for (int i = 1; i < entries; ++i){ | |
add_to_list(&test_link, rand()); | |
if( debug == 1 ) | |
print_list(test_link); | |
} | |
int members = entries; | |
for (int i = 0; i < entries; ++i){ | |
if( members == 1 ){ | |
remove_from_list(&test_link, 0); | |
}else{ | |
remove_from_list(&test_link, rand() % (members-1)); | |
} | |
members--; | |
if( debug == 1 ) | |
print_list(test_link); | |
} | |
Seconds link_end = GetTimeNow(); // get time now | |
double vec_seconds = vec_end - vec_start; | |
double list_seconds = list_end - list_start; | |
double link_seconds = link_end - link_start; | |
std::cout<<"Vector ran in "<<vec_seconds<<" seconds\nList ran in "<<list_seconds<<" Seconds\nLink ran in "<<link_seconds<<" Seconds\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This isn't iterating anything: