Skip to content

Instantly share code, notes, and snippets.

@nicolandu
Created July 18, 2024 14:53
Show Gist options
  • Save nicolandu/2b96efebf20b8cd2aaff6d91587f67b8 to your computer and use it in GitHub Desktop.
Save nicolandu/2b96efebf20b8cd2aaff6d91587f67b8 to your computer and use it in GitHub Desktop.
Testing maximum recursion in Python on various online code running platforms.
# PYTHON MAX RECURSION ANALYSIS
import sys
sys.setrecursionlimit(2147483647) # If larger than 2^31-1 (INT_MAX): OverflowError: Python int too large to convert to C int
import tracemalloc
import time
tracemalloc.start()
init = time.perf_counter()
# inner psutil function
def process_memory():
return tracemalloc.get_traced_memory()[0] # (returns (current, peak), only take current)
def tracing_test(x):
print(x, process_memory(), time.perf_counter()-init)
try:
return tracing_test(x+1)
except RecursionError:
return x
def non_tracing_test(x):
print(x)
try:
return non_tracing_test(x+1)
except RecursionError:
return x
tracing_test(1)
#non_tracing_test(1)
# Results: W3Schools.com caps at:
# x=158729 without mem/time tracing
# x=29365 with tracing, 14257416 B memory usage, 0.1451405994594097 seconds
# online-python.com caps at:
# x=16876 without mem/time tracing
# x=17266 with tracing, 8406514 B memory usage, 0.7222671359777451 seconds
# onlinegdb.com caps at:
# x=17454 without mem/time tracing
# x=17465 with tracing, 8305968 B memory usage, 30.362882519024424 seconds
# jdoodle.com caps at:
# x=1655 without mem/time tracing
# x=230 with tracing, 51801 B memory usage, 0.004090732996701263 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment