From 1388e7f555ba95c2559e2c963ef664d40a578d0e Mon Sep 17 00:00:00 2001
From: bartvbl <bartvblokl@gmail.com>
Date: Tue, 4 Mar 2025 13:35:54 +0100
Subject: [PATCH] Handout lecture 11

---
 .../Stopwatch.cpp                             | 12 +++++++++
 .../Stopwatch.h                               | 19 ++++++++++++++
 .../main.cpp                                  | 25 +++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.cpp
 create mode 100644 templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.h
 create mode 100644 templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/main.cpp

diff --git a/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.cpp b/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.cpp
new file mode 100644
index 0000000..a4be1c7
--- /dev/null
+++ b/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.cpp	
@@ -0,0 +1,12 @@
+#include "Stopwatch.h"
+
+void Stopwatch::start() {
+    startTime = std::chrono::steady_clock::now();
+}
+
+double Stopwatch::stop() {
+    std::chrono::time_point endTime = std::chrono::steady_clock::now();
+    long durationInMicroseconds = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
+    double durationInSeconds = double(durationInMicroseconds)/1000000.0;
+    return durationInSeconds;
+}
\ No newline at end of file
diff --git a/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.h b/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.h
new file mode 100644
index 0000000..5d7b1c3
--- /dev/null
+++ b/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/Stopwatch.h	
@@ -0,0 +1,19 @@
+#pragma once
+#include <chrono>
+
+// This class abstracts some (somewhat) nasty code that is
+// definitely outside the scope of this course.
+// Its main purpose is to return the amount of time 
+// taken by the program in main.cpp
+
+// Calling start() starts the stopwatch
+// Calling stop() stops it and returns the amount of time
+// that has elapsed since start() was called in seconds
+
+class Stopwatch {
+    std::chrono::time_point<std::chrono::steady_clock> startTime;
+
+public:
+    void start();
+    double stop();
+};
\ No newline at end of file
diff --git a/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/main.cpp b/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/main.cpp
new file mode 100644
index 0000000..bf12b73
--- /dev/null
+++ b/templates/_folder_Lectures/_folder_Lecture 11/Handout - Performance Optimisation/main.cpp	
@@ -0,0 +1,25 @@
+#include <iostream>
+#include <chrono>
+#include <vector>
+#include "Stopwatch.h"
+
+int main() {
+    Stopwatch watch;
+    watch.start();
+
+    constexpr int gridSize = 25000;
+
+    std::vector<std::array<int, gridSize>> grid(gridSize);
+
+    long long sum = 0;
+    for(int row = 0; row < gridSize; row++) {
+        for(int col = 0; col < gridSize; col++) {
+            //std::string hello = "Hello there!";
+            sum += grid.at(row).at(col);
+        }
+    }
+
+    std::cout << "Time taken: " << watch.stop() << " seconds" << std::endl;
+
+    return 0;
+}
\ No newline at end of file
-- 
GitLab