initial
This commit is contained in:
commit
aa9bd22bde
6
CMakeLists.txt
Normal file
6
CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
project(threads)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(threads main.cpp)
|
||||
97
main.cpp
Normal file
97
main.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
std::vector<int> read_measurements(const std::string& filename);
|
||||
void parallel_measurements(std::vector<int> measurements);
|
||||
void serial_measurements(std::vector<int> measurements);
|
||||
|
||||
int main() {
|
||||
std::vector<int> measurements = read_measurements(R"(C:\Users\tilob\CLionProjects\threads\measurements.txt)");
|
||||
|
||||
// test parallel
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
parallel_measurements(measurements);
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
std::chrono::duration<double> diff = end - start;
|
||||
std::cout << "Parallel Took: " << diff.count() << "s\n";
|
||||
|
||||
// test serial
|
||||
start = std::chrono::steady_clock::now();
|
||||
serial_measurements(measurements);
|
||||
end = std::chrono::steady_clock::now();
|
||||
diff = end - start;
|
||||
std::cout << "Serial Took: " << diff.count() << "s\n";
|
||||
}
|
||||
|
||||
void parallel_measurements(std::vector<int> measurements) {
|
||||
int numThreads = 4;
|
||||
std::vector<int> results(numThreads, 0);
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
int totalComparisons = measurements.size() - 1;
|
||||
int base = totalComparisons / numThreads;
|
||||
int remainder = totalComparisons % numThreads;
|
||||
|
||||
int idx = 1;
|
||||
for (int t = 0; t < numThreads; t++)
|
||||
{
|
||||
int startIdx = idx;
|
||||
|
||||
// wie viele Vergleiche dieser Thread macht
|
||||
int count = base + (t < remainder ? 1 : 0);
|
||||
|
||||
int endIdx = idx + count - 1;
|
||||
idx = endIdx + 1;
|
||||
|
||||
// Lambda-Funktion: definiert die Aufgabe, die dieser Thread ausführt
|
||||
threads.emplace_back([&, t, startIdx, endIdx]()
|
||||
{
|
||||
int localCount = 0;
|
||||
for (int i = startIdx; i <= endIdx; ++i)
|
||||
if (measurements[i] > measurements[i - 1])
|
||||
++localCount;
|
||||
|
||||
results[t] = localCount;
|
||||
});
|
||||
}
|
||||
|
||||
// auf jeden Thread warten
|
||||
for (auto &th : threads)
|
||||
th.join();
|
||||
|
||||
// Ergebnisse aufsummieren
|
||||
int total = 0;
|
||||
for (int r : results)
|
||||
total += r;
|
||||
|
||||
std::cout << "Anzahl steigender Messwerte (parallel): " << total << "\n";
|
||||
}
|
||||
|
||||
std::vector<int> read_measurements(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
std::vector<int> result;
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Could not open file\n";
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
// Process each line
|
||||
result.push_back(std::stoi(line));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void serial_measurements(std::vector<int> measurements) {
|
||||
int result = 0;
|
||||
for (size_t i = 1; i < measurements.size(); ++i) {
|
||||
if (measurements[i] > measurements[i - 1])
|
||||
++result;
|
||||
}
|
||||
}
|
||||
2000
measurements.txt
Normal file
2000
measurements.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user