Skip to content

Commit

Permalink
Move benchmarks into function
Browse files Browse the repository at this point in the history
This makes it easier to disable some of the
tests while working on a particular optimization.
  • Loading branch information
ronaldoussoren committed May 20, 2021
1 parent 4162b20 commit c6c9acf
Showing 1 changed file with 71 additions and 54 deletions.
125 changes: 71 additions & 54 deletions pyobjc-core/Tools/pyobjcbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""
from __future__ import print_function

import textwrap
import timeit

import objc
Expand All @@ -32,57 +33,73 @@ def print_bench(title, time):
NSObject = objc.lookUpClass("NSObject")
NSArray = objc.lookUpClass("NSArray")

print_bench(
"object description lookup", timeit.timeit(setup="o = object()", stmt="o.__repr__")
)
print_bench(
"NSObject description lookup",
timeit.timeit(
setup='import objc; NSObject = objc.lookUpClass("NSObject"); '
"o = NSObject.alloc().init()",
stmt="o.description",
),
)
print_bench(
"NSArray description lookup",
timeit.timeit(
setup='import objc; NSArray = objc.lookUpClass("NSArray"); '
"o = NSArray.alloc().init()",
stmt="o.description",
),
)
print()
print_bench(
"object description call",
timeit.timeit(setup="m = object().__repr__", stmt="m()"),
)
print_bench(
"NSObject description call",
timeit.timeit(
setup='import objc; NSObject = objc.lookUpClass("NSObject"); '
"m = NSObject.alloc().init().description",
stmt="m()",
),
)
print_bench(
"NSArray description call",
timeit.timeit(
setup='import objc; NSArray = objc.lookUpClass("NSArray"); '
"m = NSArray.alloc().init().description",
stmt="m()",
),
)
print()
print_bench(
"python function call",
timeit.timeit(setup="import math; f=math.sin", stmt="f(5.0)"),
)
setup = """
import objc
objc.loadBundleFunctions(None, globals(), [("sin", b"dd")])
f = sin
"""
print_bench(
"objc function call",
timeit.timeit(setup=setup, stmt="f(5.0)"),
)

def descriptor_lookup():
print_bench(
"object description lookup",
timeit.timeit(setup="o = object()", stmt="o.__repr__"),
)
print_bench(
"NSObject description lookup",
timeit.timeit(
setup='import objc; NSObject = objc.lookUpClass("NSObject"); '
"o = NSObject.alloc().init()",
stmt="o.description",
),
)
print_bench(
"NSArray description lookup",
timeit.timeit(
setup='import objc; NSArray = objc.lookUpClass("NSArray"); '
"o = NSArray.alloc().init()",
stmt="o.description",
),
)
print()


def descriptor_call():
print_bench(
"object description call",
timeit.timeit(setup="m = object().__repr__", stmt="m()"),
)
print_bench(
"NSObject description call",
timeit.timeit(
setup='import objc; NSObject = objc.lookUpClass("NSObject"); '
"m = NSObject.alloc().init().description",
stmt="m()",
),
)
print_bench(
"NSArray description call",
timeit.timeit(
setup='import objc; NSArray = objc.lookUpClass("NSArray"); '
"m = NSArray.alloc().init().description",
stmt="m()",
),
)
print()


def function_call():
print_bench(
"python function call",
timeit.timeit(setup="import math; f=math.sin", stmt="f(5.0)"),
)
setup = textwrap.dedent(
"""\
import objc
objc.loadBundleFunctions(None, globals(), [("sin", b"dd")])
f = sin
"""
)
print_bench(
"objc function call",
timeit.timeit(setup=setup, stmt="f(5.0)"),
)


descriptor_lookup()
descriptor_call()
function_call()

0 comments on commit c6c9acf

Please sign in to comment.