Profiling
C++ is a compiled programming language. So you will need to compile your program in such a way, that your profiler can know what is happening.
Assignments
You should try the following assignments with the different profiling tools you will learn today. The code example will work on Linux and should work on MacOS. If you are using Windows try WSL, a VM or the Linux-Remote-Maschine of the university.
Compare busybox sort and GNU sort
You will need busybox sort on your machine for this. Install it from your preferred package manager or load it from busybox itself. Alternatively implement your own sorting function (or multiple).
shuf -i 0-999999 -n 1000 > thousand
shuf -i 0-999999 -n 1000000 > million
Compare runtime (the > /dev/null will hide the output. The interesting part is the user time.)
time busybox sort -n million > /dev/null
time sort -n million > /dev/null
Compare the profiling
<profiler> busybox sort -n thousand > /dev/null
<profiler> sort -n thousand > /dev/null
Compare your own implementation of algorithm
functions with std
For example try to write a min_element
function for an array and compare it to min_element.
You can create a vector
with random numbers with the following code:
#include <algorithm>
#include <limits>
#include <random>
#include <type_traits>
#include <vector>
template <typename T>
static std::vector<T> generate_float_data(size_t size) {
static std::default_random_engine generator;
std::vector<T> data(size);
static std::uniform_real_distribution<T> distribution(
std::numeric_limits<T>::min(),
std::numeric_limits<T>::max());
std::generate(data.begin(), data.end(), []() { return distribution(generator); });
return data;
}
template <typename T>
static std::vector<T> generate_int_data(size_t size) {
static std::default_random_engine generator;
std::vector<T> data(size);
static std::uniform_int_distribution<T> distribution(
std::numeric_limits<T>::min(),
std::numeric_limits<T>::max());
std::generate(data.begin(), data.end(), []() { return distribution(generator); });
return data;
}
Things you could try to implement:
Compare runtime of different data types
This is more just for fun. Try some of the functions from the assignment before but use different data types like
uint32_t
vsuint64_t
int16_t
vsint_fast16_t
float
vsint64_t
vsdouble