-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming_with_graph.py
248 lines (209 loc) · 8.65 KB
/
timing_with_graph.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import timeit
import statistics
from task_i_pandas import read_input, merge, transform, make_output
import matplotlib.pyplot as plt
import pandas as pd
from tabulate import tabulate # For nice table formatting
def pandas_approach(input_size: int):
SETUP_CODE = {
'merge': f'''
from task_i_pandas import read_input, merge, transform, make_output
input_df = read_input()
area_df, date_dim_df, dwelling_df, electricity_df = input_df
electricity_df.iloc[:{input_size}]
''',
'transform': f'''
from task_i_pandas import read_input, merge, transform, make_output
input_df = read_input()
area_df, date_dim_df, dwelling_df, electricity_df = input_df
electricity_df.iloc[:{input_size}]
merged_df = merge(area_df, date_dim_df, dwelling_df, electricity_df)
''',
'make_output': f'''
from task_i_pandas import read_input, merge, transform, make_output
input_df = read_input()
area_df, date_dim_df, dwelling_df, electricity_df = input_df
electricity_df.iloc[:{input_size}]
merged_df = merge(area_df, date_dim_df, dwelling_df, electricity_df)
denormalized_df = transform(merged_df)
'''
}
TEST_CODES = {
'merge': 'merged_df = merge(area_df, date_dim_df, dwelling_df, electricity_df)',
'transform': 'denormalized_df = transform(merged_df)',
'make_output': 'result = make_output(denormalized_df)'
}
timing_results = {}
# time multiple functions
for operation in ['merge', 'transform', 'make_output']:
times = timeit.repeat(
setup=SETUP_CODE[operation],
stmt=TEST_CODES[operation],
repeat=2,
number=5
)
timing_results[operation] = {
'min': min(times),
'max': max(times),
'avg': statistics.mean(times),
'std': statistics.stdev(times)
}
print("\nTiming Results for Pandas Approach(in seconds):")
for operation, results in timing_results.items():
print(f"\n{operation.upper()}:")
print(f"Minimum: {results['min']:.4f}")
print(f"Maximum: {results['max']:.4f}")
print(f"Average: {results['avg']:.4f}")
print(f"Std Dev: {results['std']:.4f}")
return timing_results
def pure_python_approach(input_size: int):
SETUP_CODE = {
'merge': f'''
from task_i_non_pandas import read_input, merge, transform, make_output
input_df = read_input()
area_data, date_dim_data, dwelling_data, electricity_data = input_df
electricity_data = electricity_data[:{input_size}]
''',
'transform': f'''
from task_i_non_pandas import read_input, merge, transform, make_output
input_df = read_input()
area_data, date_dim_data, dwelling_data, electricity_data = input_df
electricity_data = electricity_data[:{input_size}]
merged_data = merge(area_data, date_dim_data, dwelling_data, electricity_data)
''',
'make_output': f'''
from task_i_non_pandas import read_input, merge, transform, make_output
input_df = read_input()
area_data, date_dim_data, dwelling_data, electricity_data = input_df
electricity_data = electricity_data[:{input_size}]
merged_data = merge(area_data, date_dim_data, dwelling_data, electricity_data)
denormalized_data = transform(merged_data)
'''
}
TEST_CODES = {
'merge': 'merged_data = merge(area_data, date_dim_data, dwelling_data, electricity_data)',
'transform': 'denormalized_data = transform(merged_data)',
'make_output': 'result = make_output(denormalized_data)'
}
timing_results = {}
# time multiple functions
for operation in ['merge', 'transform', 'make_output']:
times = timeit.repeat(
setup=SETUP_CODE[operation],
stmt=TEST_CODES[operation],
repeat=2,
number=5
)
timing_results[operation] = {
'min': min(times),
'max': max(times),
'avg': statistics.mean(times),
'std': statistics.stdev(times)
}
return timing_results
if __name__ == "__main__":
# setup from task_i_pandas.py
input_df = read_input()
merged_df = merge(input_df[0], input_df[1], input_df[2], input_df[3])
denormalized_df = transform(merged_df)
data = make_output(denormalized_df)
max_input_size = len(data)
# got these sizes from looking at the data earlier
input_sizes = [1000, 5000, 10000, 15000, 20000, max_input_size]
pandas_timing_sizes = []
pure_python_timing_sizes = []
# run both approaches and collect timings
for size in input_sizes:
print(f"\nTesting with input size: {size}")
pandas_result = pandas_approach(size)
pandas_timing_sizes.append(pandas_result)
pure_python_result = pure_python_approach(size)
pure_python_timing_sizes.append(pure_python_result)
# setup the plots side by side
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 8))
operations = ['merge', 'transform', 'make_output']
colors = ['b', 'g', 'r']
# find the max y value across both approaches to sync the scales
max_time = max(
max(result[op]['avg'] for result in pandas_timing_sizes for op in operations),
max(result[op]['avg'] for result in pure_python_timing_sizes for op in operations)
)
y_limit = max_time * 1.1 # add 10% padding
# plot pandas results
for op, color in zip(operations, colors):
avg_times = [result[op]['avg'] for result in pandas_timing_sizes]
ax1.plot(input_sizes, avg_times, f'{color}o-', label=op.capitalize())
for i, (size, time) in enumerate(zip(input_sizes, avg_times)):
ax1.annotate(f'{time:.2f}s',
(size, time),
textcoords="offset points",
xytext=(0,10),
ha='center')
ax1.set_xlabel('Input Size')
ax1.set_ylabel('Average Time (seconds)')
ax1.set_title('Pandas Approach: Time vs Input Size')
ax1.set_ylim(0, y_limit) # sync y axis
ax1.legend()
ax1.grid(True)
# plot pure python results
for op, color in zip(operations, colors):
avg_times = [result[op]['avg'] for result in pure_python_timing_sizes]
ax2.plot(input_sizes, avg_times, f'{color}o-', label=op.capitalize())
for i, (size, time) in enumerate(zip(input_sizes, avg_times)):
ax2.annotate(f'{time:.2f}s',
(size, time),
textcoords="offset points",
xytext=(0,10),
ha='center')
ax2.set_xlabel('Input Size')
ax2.set_ylabel('Average Time (seconds)')
ax2.set_title('Pure Python Approach: Time vs Input Size')
ax2.set_ylim(0, y_limit) # sync y axis
ax2.legend()
ax2.grid(True)
def create_comparison_table(pandas_results, python_results, input_sizes):
comparison_data = []
for i, size in enumerate(input_sizes):
for op in operations:
# round everything to 2 decimals for cleaner output
pandas_time = round(pandas_results[i][op]['avg'], 2)
python_time = round(python_results[i][op]['avg'], 2)
diff = round(python_time - pandas_time, 2)
speedup = round(python_time / pandas_time, 2)
comparison_data.append({
'Input Size': f"{size:,}",
'Operation': op.capitalize(),
'Pandas Avg (s)': pandas_time,
'Python Avg (s)': python_time,
'Difference (s)': diff,
'Speedup Factor': f"{speedup}x"
})
df = pd.DataFrame(comparison_data)
formatted_table = tabulate(
df,
headers={
'Input Size': 'Size',
'Operation': 'Operation',
'Pandas Avg (s)': 'Pandas (s)',
'Python Avg (s)': 'Python (s)',
'Difference (s)': 'Diff (s)',
'Speedup Factor': 'Speedup'
},
tablefmt='pretty',
floatfmt=('', '', '.2f', '.2f', '.2f', '.2f')
)
return formatted_table
print("\nPerformance Comparison Summary:")
print(create_comparison_table(pandas_timing_sizes, pure_python_timing_sizes, input_sizes))
print("\nOverall Performance Summary:")
print("-" * 50)
for size in input_sizes:
idx = input_sizes.index(size)
pandas_total = sum(pandas_timing_sizes[idx][op]['avg'] for op in operations)
python_total = sum(pure_python_timing_sizes[idx][op]['avg'] for op in operations)
print(f"\nInput Size: {size}")
print(f"Total Pandas Time: {pandas_total:.4f}s")
print(f"Total Python Time: {python_total:.4f}s")
print(f"Overall Speedup: {python_total/pandas_total:.2f}x")
plt.tight_layout()
plt.show()