Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import time
import sys
import os

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

def get_memory_usage():
"""
Returns the current memory usage of the process in MB.
"""
try:
with open('/proc/self/stat', 'r') as f:
stats = f.read().split()
vsize = int(stats[22]) # Virtual memory size in bytes
return vsize / (1024 ** 2) # Convert to MB
except (IOError, IndexError):
return 0.0 # Fallback to 0 if the memory usage cannot be determined

def measure_execution_time_and_memory(func, *args, **kwargs):
"""
Measures the execution time and memory usage of a function.
"""
start_time = time.time()
start_memory = get_memory_usage()

result = func(*args, **kwargs)

end_time = time.time()
end_memory = get_memory_usage()

execution_time = end_time - start_time
memory_usage = end_memory - start_memory

print(f"Result: {result}")
print(f"Execution Time: {execution_time:.4f} seconds")
print(f"Memory Usage: {memory_usage:.2f} MB")

if __name__ == "__main__":
n = int(sys.argv[1]) if len(sys.argv) > 1 else 10
measure_execution_time_and_memory(factorial, n)