-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
94 lines (68 loc) · 2.22 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
#
# This file is part of EventGhost.
# Copyright © 2005-2018 EventGhost Project <http://eventghost.net/>
#
# EventGhost is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 2 of the License, or (at your option)
# any later version.
#
# EventGhost is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with EventGhost. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import timeit
from pyOverloadedMethods import OverloadedClass, overload
PRINT = True
class TestClass(OverloadedClass):
@overload
def test1(self, test1=int()):
if PRINT:
print('int called', test1)
@overload
def test1(self, test1=dict()):
if PRINT:
print('dict called', test1)
@overload
def test1(self, test1=float()):
if PRINT:
print('float called', test1)
t_class = TestClass()
t_class.test1(test1=1)
t_class.test1(test1=dict(test='test'))
t_class.test1(test1=1.45)
print(t_class.test1)
PRINT = False
COUNT = 0
code = '''
t_class.test1(test1=COUNT)
t_class.test1(test1=dict(test=COUNT))
t_class.test1(test1=0.45 + COUNT)
globals()['COUNT'] += 1
'''
test1 = timeit.timeit(code, number=10000, globals=locals())
class TestClass(object):
def test1(self, test1):
if isinstance(test1, int):
if PRINT:
print('int called', test1)
if isinstance(test1, dict):
if PRINT:
print('dict called', test1)
if isinstance(test1, float):
if PRINT:
print('float called', test1)
t_class = TestClass()
t_class.test1(test1=1)
t_class.test1(test1=dict(test='test'))
t_class.test1(test1=1.45)
COUNT = 0
test2 = timeit.timeit(code, number=10000, globals=locals())
print('test1:', test1)
print('test2:', test2)
print('overrides are slower by:', test1 - test2, 'over 10000 calls')