add timers

This commit is contained in:
2026-04-21 10:54:57 +07:00
parent 099491dffa
commit 1c1c52786c
2 changed files with 35 additions and 10 deletions
BIN
View File
Binary file not shown.
+35 -10
View File
@@ -15,6 +15,13 @@ struct SharedData {
int arr[MAX];
};
// ================= TIME =================
double now() {
timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + t.tv_usec / 1e6;
}
// ================= MERGE =================
void merge(int *arr, int l, int m, int r) {
int *temp = new int[r - l + 1];
@@ -50,14 +57,27 @@ void local_sort(int *arr, int l, int r) {
void parallel_sort(int *arr, int l, int r, int depth) {
if (l >= r) return;
int m = (l + r) / 2;
double start = now();
cout << "PID " << getpid()
pid_t pid = getpid();
pid_t ppid = getppid();
cout << "[START] t=" << start
<< " PID=" << pid
<< " PPID=" << ppid
<< " depth=" << depth
<< " range=[" << l << "," << r << "]\n";
int m = (l + r) / 2;
if (depth >= MAX_DEPTH) {
local_sort(arr, l, r);
double end = now();
cout << "[END] t=" << end
<< " PID=" << pid
<< " duration=" << (end - start)
<< " sec\n";
return;
}
@@ -79,6 +99,13 @@ void parallel_sort(int *arr, int l, int r, int depth) {
waitpid(right, NULL, 0);
merge(arr, l, m, r);
double end = now();
cout << "[END] t=" << end
<< " PID=" << pid
<< " duration=" << (end - start)
<< " sec\n";
}
// ================= MAIN =================
@@ -95,19 +122,17 @@ int main() {
for (int i = 0; i < n; i++)
data->arr[i] = rand() % 100000;
timeval start, end;
double global_start = now();
gettimeofday(&start, NULL);
cout << "Main PID=" << getpid() << "\n\n";
parallel_sort(data->arr, 0, n - 1, 0);
gettimeofday(&end, NULL);
double global_end = now();
double time_spent =
(end.tv_sec - start.tv_sec) +
(end.tv_usec - start.tv_usec) / 1e6;
cout << "\nExecution time: " << time_spent << " sec\n";
cout << "\nExecution time: "
<< (global_end - global_start)
<< " sec\n";
// Проверка
for (int i = 1; i < n; i++) {