Skip to content

Commit dd72d01

Browse files
committed
Update code timing
1 parent 9e5346f commit dd72d01

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

content/posts/cpp-learn.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,87 @@ See plus plus :) .
8383
- `float``%f`
8484
- Use `snprintf()` with correctly sized buffer to avoid overflow
8585

86+
#### Code Timming
87+
- C++11 comes with some functionality in the chrono library to time our code to see how long it takes to run.
88+
- e.g.
89+
```cpp
90+
#include <array>
91+
#include <chrono> // for std::chrono functions
92+
#include <cstddef> // for std::size_t
93+
#include <iostream>
94+
#include <numeric> // for std::iota
95+
96+
const int g_arrayElements { 10000 };
97+
98+
class Timer
99+
{
100+
private:
101+
// Type aliases to make accessing nested type easier
102+
using Clock = std::chrono::steady_clock;
103+
using Second = std::chrono::duration<double, std::ratio<1> >;
104+
105+
std::chrono::time_point<Clock> m_beg{ Clock::now() };
106+
107+
public:
108+
109+
void reset()
110+
{
111+
m_beg = Clock::now();
112+
}
113+
114+
double elapsed() const
115+
{
116+
return std::chrono::duration_cast<Second>(Clock::now() - m_beg).count();
117+
}
118+
};
119+
120+
void sortArray(std::array<int, g_arrayElements>& array)
121+
{
122+
123+
// Step through each element of the array
124+
// (except the last one, which will already be sorted by the time we get there)
125+
for (std::size_t startIndex{ 0 }; startIndex < (g_arrayElements - 1); ++startIndex)
126+
{
127+
// smallestIndex is the index of the smallest element we’ve encountered this iteration
128+
// Start by assuming the smallest element is the first element of this iteration
129+
std::size_t smallestIndex{ startIndex };
130+
131+
// Then look for a smaller element in the rest of the array
132+
for (std::size_t currentIndex{ startIndex + 1 }; currentIndex < g_arrayElements; ++currentIndex)
133+
{
134+
// If we've found an element that is smaller than our previously found smallest
135+
if (array[currentIndex] < array[smallestIndex])
136+
{
137+
// then keep track of it
138+
smallestIndex = currentIndex;
139+
}
140+
}
141+
142+
// smallestIndex is now the smallest element in the remaining array
143+
// swap our start element with our smallest element (this sorts it into the correct place)
144+
std::swap(array[startIndex], array[smallestIndex]);
145+
}
146+
}
147+
148+
int main()
149+
{
150+
std::array<int, g_arrayElements> array;
151+
std::iota(array.rbegin(), array.rend(), 1); // fill the array with values 10000 to 1
152+
153+
Timer t;
154+
155+
sortArray(array);
156+
157+
std::cout << "Time taken: " << t.elapsed() << " seconds\n";
158+
159+
return 0;
160+
}
161+
```
86162
163+
- **Things that can impact the performance of the program:** TBD
164+
- **Measuring performance:**
165+
- gather at least 3 results.
166+
- the program runs in 10 seconds etc
87167
## 1. Introduction
88168
- C++ was developed as an extension to C. It adds man few features to the C language, and tis perhaps best through of as a superset of C.
89169

0 commit comments

Comments
 (0)