Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Measuring CPU Time in C++: An Analysis of Vectors and Lists - Prof. Michael H. Goldwasser, Lab Reports of Data Structures and Algorithms

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

2009/2010

Uploaded on 02/24/2010

koofers-user-7dt-1
koofers-user-7dt-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Keeping track of CPU time in C++
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 tis a datatype for measuring the size of a container).
It then provides a function clock() that returns a clock tinstance 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 Nnumbers 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.
Trying this with a vector and with a list.
Performing the insertion at the front of the container or the back of the container.
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.
pf2

Partial preview of the text

Download Measuring CPU Time in C++: An Analysis of Vectors and Lists - Prof. Michael H. Goldwasser and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Keeping track of CPU time in C++

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.

  • Trying this with a vector and with a list.
  • Performing the insertion at the front of the container or the back of the container.

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 back of a vector

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, 
  • 10,240,
  • 20,480,
  • 40,960,
  • 81,920,
  • 163,840,
  • 327,680, - 10, N cumulative average worst - 20, - 40, - 80, - 160, - 320, - 640, - 1,280, - 2,560, - 5,120, - 10,240, - 20,480, - 40,960, - 81,920, - 163,840, - 327,680, - 10, N cumulative average worst - 20, - 40, - 80, - 160, - 320, - 640, - 1,280, - 2,560, - 5,120,
    • 10,240,
    • 20,480,
    • 40,960,
    • 81,920,
  • 163,840,
  • 327,680, - 10, N cumulative average worst - 20, - 40, - 80, - 160, - 320, - 640, - 1,280, - 2,560, - 5,120, - 10,240, - 20,480, - 40,960, - 81,920, - 163,840, - 327,680,