

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Instructions on how to use the time.h library in c++ to measure cpu time and conduct experiments on the efficiency of vectors and lists. The goal is to compare the performance of inserting elements at the front and back of both data structures. Students are encouraged to gather statistics and draw conclusions based on the results.
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!
There are several libraries that allow for the tracking of time from within a C++ program. For today, we will rely on the following:
#include <time.h>
This library defines a new data type clock t that is an integer-like value that represents a quantity of clock ticks (similar to how size t is a datatype for measuring the size of a container). It then provides a function clock() that returns a clock t instance describing the number of clock ticks devoted to the execution of the current program. The number of clock ticks per second depends upon the machine architecture; that value can be found using the defined CLOCKS PER SEC. To measure the time used for a specific portion of your program, the typical approach used is to record the starting time, then do the work, then record the stopping time, and then calculate the difference between the start and stop times. For example
clock_t begin, end; begin = clock(); // Go! ... // Do something end = clock(); // Stop! double elapsed = (end - begin) / ((double) CLOCKS_PER_SEC); // measured in seconds
For today’s lab, our goal is to gather statistics about the efficiency of C++ vectors and lists, hoping to see evidence of the amortized nature of vectors and the relative efficiency of lists. The experiment setup should be to run a loop to insert N numbers into a container, recording the time it takes for each individual insertion and then computing the overall maximum insertion time as well as the overall average insertion time. Furthermore, we wish to vary the experiment along two axes.
Note that both the vector and list classes support the push back method for adding a new element at the end. While the list supports push front as well, this is not supported by a vector (for good reason, as we will see). A syntax that can be used for both the vector and the list for inserting at the front is data.insert(data.begin(), item), assuming that data is the name of the container.
The reverse of this page gives charts for you to fill out. We doubt you will get to complete all of the charts, but do your best to gather enough evidence to draw a conclusion about the efficiency of those behaviors.
Inserting at the front of a vector inserting at the back of a list inserting at the front of a list
- 10, N cumulative average worst - 20, - 40, - 80, - 160, - 320, - 640, - 1,280, - 2,560, - 5,120,