/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2016-2023 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** array.cxx : measure speed of array access **** ****************************************************************/ #include #include // for memcpy #include using std::cin; using std::cout; int main() { #define NEXPERIMENTS 10 std::chrono::system_clock::time_point point; /* * We expand an array by reallocating. * We do this for various maximum sizes, * and at each size multiple experiments. */ cout << "Reallocation" << '\n'; for (size_t size=10; size<100000000; size*=10) { cout << "Maximum size: " << size << '\n'; point = std::chrono::system_clock::now(); double *a1; double total=0.; for (int x=0; x(duration); cout << "time: " << millisec_duration.count()*1.e-3 << '\n'; } /* * Now do basically the same thing with a linked list */ { class Node { private: int data; Node *next; public: Node(int i) { data = i; next = nullptr; }; bool hastail() { return next!=nullptr; }; Node *tail() { return next; }; void settail( Node *n) { next = n; }; }; class List { private: Node *head; public: List() { head = new Node(0); }; void zap() { head = new Node(0); }; void append( Node *n ) { Node *cur = head; while (cur->hastail()) cur = cur->tail(); cur->settail(n); }; }; cout << "Linked list" << '\n'; for (size_t size=10; size<1000000; size*=10) { List list; cout << "Maximum size: " << size << '\n'; point = std::chrono::system_clock::now(); for (int x=0; x(duration); cout << "time: " << millisec_duration.count()*1.e-3 << '\n'; } } return 0; }