Skip to main content

7.5) Exercise 13


Look at the example program insertvec.cpp. This program performs a user-specified number of insertions at the front of a vector of integers and measures the approximate CPU time required to do this. Compile the program in the usual way, then run it like so (omit the ./ if using the Windows Command prompt):

./insertvec 40000

Repeat for 80,000, 120,000 and 160,000 insertions. Notice how rapidly execution time increases with the number of insertions.

Now look at example program insertlist.cpp. This is the equivalent program using list instead of vector. Compile and run it with the same parameters. You should see a big difference in execution times!

Depending on the speed of your PC, you may find that execution time fails to register at all in these trials; if so, try a much larger number of insertions - e.g., 500,000 or 1 million).