-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample5_comprehensive.c
More file actions
166 lines (134 loc) · 4.02 KB
/
example5_comprehensive.c
File metadata and controls
166 lines (134 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Example 5: Comprehensive Real-World Usage
*
* Demonstrates profiling a more realistic application with:
* - Multiple algorithms
* - Comparative performance analysis
* - Statistics collection
*/
#include "profiler.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Simulated algorithms */
void bubble_sort(int *arr, int n) {
PROFILER_FUNCTION();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void quick_sort_helper(int *arr, int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
int pi = i + 1;
quick_sort_helper(arr, low, pi - 1);
quick_sort_helper(arr, pi + 1, high);
}
}
void quick_sort(int *arr, int n) {
PROFILER_FUNCTION();
quick_sort_helper(arr, 0, n - 1);
}
void data_processing_pipeline(int size) {
PROFILER_FUNCTION();
int *data = malloc(size * sizeof(int));
{
PROFILER_SCOPE(data_generation);
for (int i = 0; i < size; i++) {
data[i] = rand() % 1000;
}
}
{
PROFILER_SCOPE(data_transformation);
for (int i = 0; i < size; i++) {
data[i] = data[i] * 2 + 1;
}
}
{
PROFILER_SCOPE(data_filtering);
int count = 0;
for (int i = 0; i < size; i++) {
if (data[i] % 3 == 0) {
count++;
}
}
}
free(data);
}
void run_performance_comparison(void) {
const int sizes[] = {100, 500, 1000};
for (int s = 0; s < 3; s++) {
int size = sizes[s];
printf("\n--- Array size: %d ---\n", size);
/* Prepare test data */
int *arr1 = malloc(size * sizeof(int));
int *arr2 = malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
arr1[i] = rand() % 1000;
arr2[i] = arr1[i];
}
/* Test bubble sort */
bubble_sort(arr1, size);
/* Test quick sort */
quick_sort(arr2, size);
free(arr1);
free(arr2);
}
}
int main(void) {
profiler_init();
srand(42);
printf("=== Example 5: Real-World Performance Analysis ===\n");
/* Enable logging */
profiler_set_log_file("performance.log");
/* Run sorting comparison */
printf("\n### Sorting Algorithm Comparison ###\n");
run_performance_comparison();
/* Run data processing pipeline */
printf("\n### Data Processing Pipeline ###\n");
data_processing_pipeline(5000);
data_processing_pipeline(10000);
/* Benchmark with statistics */
printf("\n### Statistics Collection ###\n");
profiler_stats_t pipeline_stats;
profiler_stats_init(&pipeline_stats, "pipeline_benchmark");
for (int i = 0; i < 10; i++) {
profiler_timer_t timer;
profiler_timer_start(&timer, "pipeline_iteration");
int size = 1000 + (rand() % 500);
int *data = malloc(size * sizeof(int));
for (int j = 0; j < size; j++) {
data[j] = rand() % 1000;
}
/* Simple processing */
for (int j = 0; j < size; j++) {
data[j] = data[j] * data[j];
}
free(data);
profiler_timer_stop(&timer);
profiler_stats_add(&pipeline_stats, profiler_timer_elapsed_ns(&timer));
}
printf("\n");
profiler_stats_print(&pipeline_stats);
printf("\nDetailed logs written to performance.log\n");
profiler_shutdown();
return 0;
}