from timeit import timeit
function = "def f(x): pass\n"
loop = "for x in range(1000):\n"
call = " f(x)"
# execute each test 10,000 times
params = { 'number' : 10000, 'globals': globals() }
# execution times are in seconds
# create the function before the loop
print(timeit(f'{function}{loop}{call}', **params))
# create the function inside the loop
print(timeit(f'{loop} {function}{call}', **params))
ZnJvbSB0aW1laXQgaW1wb3J0IHRpbWVpdAoKZnVuY3Rpb24gPSAiZGVmIGYoeCk6IHBhc3NcbiIKbG9vcCA9ICJmb3IgeCBpbiByYW5nZSgxMDAwKTpcbiIKY2FsbCA9ICIgICAgZih4KSIKCiMgZXhlY3V0ZSBlYWNoIHRlc3QgMTAsMDAwIHRpbWVzCnBhcmFtcyA9IHsgJ251bWJlcicgOiAxMDAwMCwgJ2dsb2JhbHMnOiBnbG9iYWxzKCkgfQoKIyBleGVjdXRpb24gdGltZXMgYXJlIGluIHNlY29uZHMKCiMgY3JlYXRlIHRoZSBmdW5jdGlvbiBiZWZvcmUgdGhlIGxvb3AKcHJpbnQodGltZWl0KGYne2Z1bmN0aW9ufXtsb29wfXtjYWxsfScsICoqcGFyYW1zKSkKIyBjcmVhdGUgdGhlIGZ1bmN0aW9uIGluc2lkZSB0aGUgbG9vcApwcmludCh0aW1laXQoZid7bG9vcH0gICAge2Z1bmN0aW9ufXtjYWxsfScsICoqcGFyYW1zKSkK