In this assignment, you have to implement a data type (class) number_series
, to store a number series (just a sequence of integers).
-
Check that the initial project works: select
All CTest
target and run, all tests should pass. Ask for help if something does not work, see if you can work with main.cpp instead, try running justmain
. -
Implement
number_series
class- Uncomment one test at a time in number_series_test.cpp, recompile and observe errors and test failures.
- Implement missing functionality (solve
TODO:
comments) in number_series.hpp until the test passes. - Use
std::vector<int>
to model data. - Maintain the minimum and the maximum values of a number series as members of your class (so that you do not have to compute them when needed).
- Implement the necessary constructors/destructors/assignment operators. Implement them only if the ones generated by default are not good. Study Section 17.6 or Item 17 in EMC++ to learn about what is generated by the compiler.
- Your class should have a static member factory function
make_random
that returns a random number series of a desired length. See uniform_int_distribution example how to generate random numbers. - Implement
operator+
andoperator+=
to add two number series element-wise. Decide yourself what to do if the number series have different lengths. - Implement
operator<
to compare the amplitudes of two number series. The amplitude is the difference between the maximum and the minimum values.
-
Solve
TODO
s in number_series_bm.cpp benchmark and runnumber_series_bm
target inRelease
profile:- Fill a vector of
100'000
random number series, each with100
elements. - Add a random number series to each of number series in the vector.
- Sort the vector according to the amplitudes of number series.
- Record how much time it takes to sort (remember to write down the timings in comments).
- Fill a vector of
-
Create a wrapper class, that has just one private data member:
std::unique_ptr<number_series>
and add the same interface as fornumber_series
. Think about the copy/move constructors/assignment operators, that you may need to provide or rely on the default ones. On the client side in number_series_bm.cpp, demonstrate that you can now use essentially the same code as in step 2.- What do you expect regarding the performance of sorting objects of this wrapper class when compared to sorting the original
number_series
objects? - Measure the performance and see whether your expectations were confirmed.
- What do you expect regarding the performance of sorting objects of this wrapper class when compared to sorting the original
-
Add
int dummy[100];
as an extra data member ofnumber_series
.- Rerun
number_series_bm
again. - How did the performance change? Why?
- Rerun
In your code, pay attention to safety and performance:
- Use
const
whenever it makes sense (arguments, member functions). - Use pass-by-reference for efficiency whenever possible.
- Avoid using plain pointers.
- Use
Release
profile (withoutDebug
info overhead) when benchmarking. To addRelease
profile in CLion visitSettings
>Build, Execution and Deployment
>CMake
>Profiles
and click+
, it should createRelease
profile automatically (no further tweaking should be needed).
number_series_test.cpp includes unit tests to guide you through the implementation.
sort_bm.cpp includes a sorting example demonstrating how to use Google Benchmark library.
number_series_bm.cpp your code for benchmarking the number_series
and number_series_wrap
.
- doctest: unit test framework.
- Google Benchmark: microbenchmark support library.
CMake should fetch those libraries automatically during cmake
reload, and the libraries should build automatically when building the project.