Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e8093509d0 |
7999
parallel/measurements.txt
Normal file
7999
parallel/measurements.txt
Normal file
File diff suppressed because it is too large
Load Diff
83
parallel/parallel_measurements.cpp
Normal file
83
parallel/parallel_measurements.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
void readFile(const std::string& filename, std::vector<int>& data) {
|
||||
std::ifstream file(filename);
|
||||
if (!file) {
|
||||
std::cerr << "Fehler: Datei '" << filename << "' konnte nicht geöffnet werden!\n";
|
||||
return;
|
||||
}
|
||||
int v;
|
||||
while (file >> v) data.push_back(v);
|
||||
}
|
||||
|
||||
int countIncreasesSerial(const std::vector<int>& data) {
|
||||
if (data.size() < 2) return 0;
|
||||
int increases = 0;
|
||||
for (size_t i = 1; i < data.size(); ++i)
|
||||
if (data[i] > data[i - 1]) ++increases;
|
||||
return increases;
|
||||
}
|
||||
|
||||
int countIncreasesParallel(const std::vector<int>& messungen, int numThreads = 4) {
|
||||
int totalComparisons = static_cast<int>(messungen.size()) - 1;
|
||||
if (totalComparisons <= 0) return 0;
|
||||
|
||||
std::vector<int> results(numThreads, 0);
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(numThreads);
|
||||
|
||||
int base = totalComparisons / numThreads;
|
||||
int remainder = totalComparisons % numThreads;
|
||||
|
||||
int idx = 1;
|
||||
for (int t = 0; t < numThreads; t++) {
|
||||
int startIdx = idx;
|
||||
int count = base + (t < remainder ? 1 : 0);
|
||||
int endIdx = idx + count - 1;
|
||||
idx = endIdx + 1;
|
||||
|
||||
if (count <= 0) {
|
||||
results[t] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
threads.emplace_back([&, t, startIdx, endIdx]() {
|
||||
int localCount = 0;
|
||||
for (int i = startIdx; i <= endIdx; ++i)
|
||||
if (messungen[i] > messungen[i - 1])
|
||||
++localCount;
|
||||
results[t] = localCount;
|
||||
});
|
||||
}
|
||||
|
||||
for (auto& th : threads) th.join();
|
||||
|
||||
int total = 0;
|
||||
for (int r : results) total += r;
|
||||
return total;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<int> messungen;
|
||||
readFile("parallel/measurements.txt", messungen);
|
||||
std::cout << "Gelesene Werte: " << messungen.size() << "\n";
|
||||
|
||||
auto t0 = std::chrono::steady_clock::now();
|
||||
int s = countIncreasesSerial(messungen);
|
||||
auto t1 = std::chrono::steady_clock::now();
|
||||
|
||||
auto p0 = std::chrono::steady_clock::now();
|
||||
int p = countIncreasesParallel(messungen, 4);
|
||||
auto p1 = std::chrono::steady_clock::now();
|
||||
|
||||
std::cout << "Seriell: " << s << " Dauer: " << std::chrono::duration<double>(t1 - t0).count() << "s\n";
|
||||
std::cout << "Parallel: " << p << " Dauer: " << std::chrono::duration<double>(p1 - p0).count() << "s\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user