I’m trying to compare GPU vs. CPU resource consumption for different operations, and I’ve written this generic function. The issue is that memory is coming back negative from this. Some investigation and GPTing indicate that some memory was freed up during the operation, potentially due to garbage collection or other system activities. Is there any alternative to this or any fix?
def measure_execution_time_and_cpu_usage(func, *args, dataset_size=None, **kwargs):
try:
# Start the timer
start_time = time.time()
# Before execution (CPU)
cpu_util_before = psutil.cpu_percent(interval=None)
cpu_memory_before = psutil.virtual_memory().used
# Execute the function
result = func(*args, **kwargs)
# After execution (CPU)
cpu_memory_after = psutil.virtual_memory().used
cpu_util_after = psutil.cpu_percent(interval=None)
# Calculate metrics
cpu_memory_used = cpu_memory_after - cpu_memory_before
execution_time = time.time() - start_time
cpu_utilization = cpu_util_after - cpu_util_before
# Calculate throughput and latency if dataset_size is provided
throughput = latency = None
if dataset_size:
throughput = dataset_size / execution_time
latency = execution_time / dataset_size
# Print metrics
print(f"CPU Memory Usage: {cpu_memory_used} bytes")
print(f"Execution Time: {execution_time} seconds")
print(f"CPU Utilization: {cpu_utilization}%")
if throughput:
print(f"Throughput: {throughput} rows/second")
print(f"Latency: {latency} seconds/row")
return result, execution_time, cpu_memory_used, throughput, latency, cpu_utilization
except Exception as e:
print(f"An error occurred: {e}")
return None, None, None, None, None, None