Skip to content

Commit e53d219

Browse files
committed
bench: Add test for function call overhead.
For a trivial operation, calling a function is 5 times slower than doing operation inline.
1 parent b0851e5 commit e53d219

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎tests/bench/funcall-1-inline.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Function call overhead test
2+
# Establish a baseline for performing a trivial operation inline
3+
import bench
4+
5+
def test(num):
6+
for i in iter(range(num)):
7+
a = i + 1
8+
9+
bench.run(test)

‎tests/bench/funcall-2-funcall.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Function call overhead test
2+
# Perform the same trivial operation as global function call
3+
import bench
4+
5+
def f(x):
6+
return x + 1
7+
8+
def test(num):
9+
for i in iter(range(num)):
10+
a = f(i)
11+
12+
bench.run(test)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Function call overhead test
2+
# Perform the same trivial operation as calling function, cached in a
3+
# local variable. This is commonly known optimization for overly dynamic
4+
# languages (the idea is to cut on symbolic look up overhead, as local
5+
# variables are accessed by offset, not by name)
6+
import bench
7+
8+
def f(x):
9+
return x + 1
10+
11+
def test(num):
12+
f_ = f
13+
for i in iter(range(num)):
14+
a = f_(i)
15+
16+
bench.run(test)

0 commit comments

Comments
 (0)