Trying more locations of the unreached code:
1631.8 ± 18.0 ns before_try
1710.2 ± 29.6 ns between_try_and_raise
294.1 ± 0.1 ns between_raise_and_except
315.6 ± 0.2 ns in_except
311.6 ± 2.2 ns after_except
So putting it anywhere before the raise makes it slow, and putting it anywhere after the raise makes it fast.
Code
from timeit import timeit
from time import perf_counter as time
from statistics import mean, stdev
import sys
def before_try():
if 0 == 1:
u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u
try:
raise RuntimeError
except RuntimeError:
pass
end = True
def between_try_and_raise():
try:
if 0 == 1:
u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u
raise RuntimeError
except RuntimeError:
pass
end = True
def between_raise_and_except():
try:
raise RuntimeError
if 0 == 1:
u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u
except RuntimeError:
pass
end = True
def in_except():
try:
raise RuntimeError
except RuntimeError:
pass
if 0 == 1:
u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u
end = True
def after_except():
try:
raise RuntimeError
except RuntimeError:
pass
if 0 == 1:
u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u;u
end = True
funcs = before_try, between_try_and_raise, between_raise_and_except, in_except, after_except
for _ in range(3):
times = {f: [] for f in funcs}
def stats(f):
ts = [t * 1e9 for t in sorted(times[f])[:5]]
return f'{mean(ts):6.1f} ± {stdev(ts):4.1f} ns '
for _ in range(500):
for f in funcs:
t = timeit(f, number=10**3) / 1e3
times[f].append(t)
for f in funcs: # sorted(funcs, key=stats):
print(stats(f), f.__name__)
print()
print('Python:', sys.version)