-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from magniff/faster_get
Faster __get__
- Loading branch information
Showing
2 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import perf | ||
|
||
|
||
from watch import WatchMe, ArrayOf, MappingOf | ||
from watch.builtins import InstanceOf | ||
|
||
|
||
class MyClass(WatchMe): | ||
foo = ArrayOf(MappingOf(InstanceOf(str), InstanceOf(int))) | ||
|
||
|
||
my_obj = MyClass() | ||
foo = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}] | ||
my_obj.foo = foo | ||
|
||
|
||
def bench_get(): | ||
return my_obj.foo | ||
|
||
|
||
def bench_set(): | ||
my_obj.foo = foo | ||
|
||
|
||
my_obj_missing = MyClass() | ||
|
||
|
||
def bench_get_missing(): | ||
try: | ||
my_obj_missing.foo | ||
except AttributeError: | ||
pass | ||
|
||
|
||
runner = perf.Runner() | ||
runner.bench_func("__get__", bench_get) | ||
runner.bench_func("__set__", bench_set) | ||
runner.bench_func("__get__missing", bench_get_missing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters