-
Notifications
You must be signed in to change notification settings - Fork 36
/
plot.py
367 lines (273 loc) · 11 KB
/
plot.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""Plot performance comparisons between mel, cmds, cmdx and PyMEL"""
import os
import sys
import timeit
import importlib
from copy import deepcopy
from maya import cmds, mel
from maya import OpenMaya as om1
from maya.api import OpenMaya as om2
from pymel import core as pm
import cmdx
try:
# Python 3 support
reload = importlib.reload
except AttributeError:
pass
try:
# Mock irrelevant pygal dependency
sys.modules["pkg_resources"] = type("Mock", (object,), {
"iter_entry_points": lambda *args, **kwargs: []
})()
import pygal
except ImportError:
raise ImportError("plot.py requires pygal")
# Results from tests end up here
data = dict()
# Precisions
Milliseconds = 0
Nanoseconds = 1
def Test(method,
task,
func,
setup=None,
teardown=None,
number=1000,
repeat=5,
precision=1):
results = list()
setup = setup or (lambda: None)
teardown = teardown or (lambda: None)
text = "%s %s: %.1f ms (%.2f {precision}/call)".format(
precision="μs" if precision else "ms"
)
for iteration in range(repeat):
setup()
results += [timeit.Timer(func).timeit(number)]
teardown()
print(text % (
task,
method,
10 ** 3 * sum(results),
10 ** (6 if precision else 3) * min(results) / number
))
if task not in data:
data[task] = {}
# Store for plot
data[task][method] = {
"func": func,
"number": number,
"results": results,
"min": sum(results),
"percall": min(results) / number
}
def New(setup=None):
cmds.file(new=True, force=True)
(setup or (lambda: None))()
def reload_pymel():
"""PyMEL consists of many submodules
PyMEL does initialisation on import.
The duration of the initialisation increases
linearly with the number of plug-ins available
on import.
"""
for mod in sys.modules.copy():
if mod.startswith("pymel"):
sys.modules.pop(mod)
import pymel.core
pymel.core # avoid linter warning
def om1GetAttr():
"""Fastest way of getting an attribute with API 2.0"""
plug = api2mfn.findPlug("translateX", False)
return plug.asDouble()
def om2GetAttr():
"""Fastest way of getting an attribute with API 2.0"""
plug = api2mfn.findPlug("translateX", False)
return plug.asDouble()
def om1SetAttr(value):
"""Fastest way of getting an attribute with API 2.0"""
plug = api2mfn.findPlug("translateX", False)
return plug.setDouble(value)
def om2SetAttr(value):
"""Fastest way of getting an attribute with API 2.0"""
plug = api2mfn.findPlug("translateX", False)
return plug.setDouble(value)
New()
Test("cmdx", "import", lambda: reload(cmdx), number=100)
Test("cmds", "import", lambda: reload(cmds), number=100)
Test("PyMEL", "import", reload_pymel, number=1)
New()
node = cmdx.createNode("transform", name="Node")
path = node.path()
pynode = pm.PyNode(path)
api1node = om1.MFnDagNode().create("transform")
api2node = om2.MFnDagNode().create("transform")
api1mfn = om1.MFnDagNode(api1node)
api2mfn = om2.MFnDagNode(api2node)
node = cmdx.createNode("transform", name="Node")
path = node.path()
pynode = pm.PyNode(path)
api1node = om1.MFnDagNode().create("transform")
api2node = om2.MFnDagNode().create("transform")
api1mfn = om1.MFnDagNode(api1node)
api2mfn = om2.MFnDagNode(api2node)
Test("cmds", "long", lambda: cmds.ls(path, long=True))
Test("cmdx", "long", lambda: node.path())
Test("PyMEL", "long", lambda: pm.ls(path, long=True))
Test("API 1.0", "long", lambda: api2mfn.fullPathName())
Test("API 2.0", "long", lambda: api2mfn.fullPathName())
Test("mel", "getAttr", lambda: mel.eval("getAttr %s" % (path + ".tx")), number=10000)
Test("cmds", "getAttr", lambda: cmds.getAttr(path + ".tx"), number=10000)
Test("cmdx", "getAttr", lambda: cmdx.getAttr(node + ".tx", type=cmdx.Double), number=10000)
Test("PyMEL", "getAttr", lambda: pynode.tx.get(), number=10000)
Test("API 1.0", "getAttr", lambda: om1GetAttr(), number=10000)
Test("API 2.0", "getAttr", lambda: om2GetAttr(), number=10000)
Test("mel", "setAttr", lambda: mel.eval("setAttr %s %s" % (path + ".tx", 5)))
Test("cmds", "setAttr", lambda: cmds.setAttr(path + ".tx", 5))
Test("cmdx", "setAttr", lambda: cmdx.setAttr(node + ".tx", 5, type=cmdx.Double))
Test("PyMEL", "setAttr", lambda: pm.setAttr(pynode + ".tx", 5))
Test("API 1.0", "setAttr", lambda: om1SetAttr(5))
Test("API 2.0", "setAttr", lambda: om2SetAttr(5))
Test("cmdx", "node.attr", lambda: node["tx"].read(), number=10000)
Test("PyMEL", "node.attr", lambda: pynode.tx.get(), number=10000)
Test("cmdx", "node.attr=5", lambda: node["tx"].write(5), number=10000)
Test("PyMEL", "node.attr=5", lambda: pynode.tx.set(5), number=10000)
Test("mel", "createNode", lambda: mel.eval("createNode \"transform\""), New)
Test("cmds", "createNode", lambda: cmds.createNode("transform"), New)
Test("cmdx", "createNode", lambda: cmdx.createNode(cmdx.Transform), New)
Test("PyMEL", "createNode", lambda: pm.createNode("transform"), New)
Test("API 1.0", "createNode", lambda: om1.MFnDagNode().create("transform"), New)
Test("API 2.0", "createNode", lambda: om2.MFnDagNode().create("transform"), New)
New()
root1 = cmdx.createNode("transform")
root2 = cmdx.createNode("transform")
child = cmdx.createNode("transform", parent=root1)
def teardown():
cmds.parent("transform3", "transform1", r=1)
melparent = 'parent -r "transform3" "transform2";'
Test("mel", "parent", lambda: mel.eval(melparent), number=1, repeat=1000, teardown=teardown)
Test("cmds", "parent", lambda: cmds.parent("transform3", "transform2", r=1), number=1, repeat=1000, teardown=teardown)
Test("cmdx", "parent", lambda: cmdx.parent(child, root2), number=1, repeat=1000, teardown=teardown)
root1, root2, child = pm.ls(map(str, (root1, root2, child)))
Test("PyMEL", "parent", lambda: pm.parent(child, root2, r=1), number=1, repeat=1000, teardown=teardown)
New()
parent = cmdx.createNode("transform")
path = parent.path()
pynode = pm.PyNode(path)
for x in range(100):
cmdx.createNode("transform", parent=parent)
Test("mel", "listRelatives", lambda: mel.eval('listRelatives -children "transform1"'))
Test("cmds", "listRelatives", lambda: cmds.listRelatives(path, children=True))
Test("cmdx", "listRelatives", lambda: cmdx.listRelatives(parent, children=True))
Test("PyMEL", "listRelatives", lambda: pm.listRelatives(pynode, children=True))
New()
root = cmdx.createNode("transform")
parent = root
path = root.path()
pynode = pm.PyNode(path)
for x in range(100):
parent = cmdx.createNode("transform", parent=parent)
Test("mel", "allDescendents", lambda: mel.eval('listRelatives -allDescendents "transform1"'))
Test("cmds", "allDescendents", lambda: cmds.listRelatives(path, allDescendents=True))
Test("cmdx", "allDescendents", lambda: cmdx.listRelatives(root, allDescendents=True))
Test("PyMEL", "allDescendents", lambda: pm.listRelatives(pynode, allDescendents=True))
New()
node1 = cmdx.createNode("transform")
node2 = cmdx.createNode("transform")
def teardown():
cmds.disconnectAttr("transform1.tx", "transform2.tx")
melconnect = 'connectAttr "transform1.tx" "transform2.tx"'
Test("mel", "connectAttr", lambda: mel.eval(melconnect), teardown=teardown, number=1, repeat=1000)
Test("cmds", "connectAttr", lambda: cmds.connectAttr("transform1.tx", "transform2.tx"), teardown=teardown, number=1, repeat=5000)
Test("cmdx", "connectAttr", lambda: cmdx.connectAttr(node1["tx"], node2["tx"]), teardown=teardown, number=1, repeat=5000)
Test("PyMEL", "connectAttr", lambda: pm.connectAttr("transform1.tx", "transform2.tx"), teardown=teardown, number=1, repeat=5000)
New()
def teardown():
cmds.deleteAttr("transform1.myAttr")
node = cmdx.createNode("transform")
path = node.path()
meladdattr = 'addAttr -ln "myAttr" -at double -dv 0 transform1;'
Test("mel", "addAttr", lambda: mel.eval(meladdattr), number=1, repeat=1000, teardown=teardown)
Test("cmds", "addAttr", lambda: cmds.addAttr(path, longName="myAttr", attributeType="double", defaultValue=0), number=1, repeat=1000, teardown=teardown)
Test("cmdx", "addAttr", lambda: cmdx.addAttr(node, longName="myAttr", attributeType=cmdx.Double, defaultValue=0), number=1, repeat=1000, teardown=teardown)
Test("PyMEL", "addAttr", lambda: pm.addAttr(path, longName="myAttr", attributeType="double", defaultValue=0), number=1, repeat=1000, teardown=teardown)
Test("cmdx", "node.addAttr", lambda: node.addAttr(cmdx.Double("myAttr")), number=1, repeat=1000, teardown=teardown)
#
# Render performance characteristics as bar charts
#
# |___
# |___|_______
# |___________|
# |______|___
# |__________|____
# |_______________|________
# Mock irrelevant pygal dependency
sys.modules["pkg_resources"] = type("Mock", (object,), {
"iter_entry_points": lambda *args, **kwargs: []
})()
def stacked(data, dirname):
data = deepcopy(data)
tasks = sorted(data.keys())
# Use a fixed order of methods in the plot
methods = ("mel", "cmds", "PyMEL", "cmdx")
# [group1 result, group2 result, ... of MEL]
# [group1 result, group2 result, ... of cmds]
# ...
cols = list()
for method in methods:
col = list()
for task in tasks:
col += [data[task].get(method, {}).get("min", 0)]
cols.append(col)
# Normalise along Y-axis
rows = zip(*cols)
for index, row in enumerate(rows[:]):
rows[index] = [100.0 * col / sum(row) for col in row]
cols = zip(*rows)
line_chart = pygal.StackedBar()
line_chart.title = "cmdx performance plot (in %)"
line_chart.x_labels = tasks
for method, col in enumerate(cols):
line_chart.add(methods[method], col)
fname = os.path.join(dirname, "stacked.svg")
line_chart.render_to_file(fname)
def horizontal(data, dirname):
data = deepcopy(data)
order = ("PyMEL", "mel", "cmds", "cmdx")
for task, methods in data.items():
chart = pygal.HorizontalBar()
chart.title = task + u" (μs)"
for method in order:
values = methods.get(method, {})
if not values:
continue
chart.add(method, 10 ** 6 * values.get("percall", 0))
fname = os.path.join(
dirname, r"%s.svg" % task
)
chart.render_to_file(fname)
def average(x, y, data):
data = deepcopy(data)
times_faster = list()
print("| | Times | Task")
print("|:--------|:------------|:------------")
for task, methods in data.items():
try:
a = methods[x]["percall"]
b = methods[y]["percall"]
except KeyError:
continue
faster = a / float(b)
print("| cmdx is | %.1fx faster | %s" % (faster, task))
times_faster.append(faster)
average = sum(times_faster) / len(times_faster)
return round(average, 2)
# Draw plots
dirname = os.path.join(os.path.dirname(cmdx.__file__), "plots")
stacked(data, dirname)
horizontal(data, dirname)
avg = average("PyMEL", "cmdx", data)
print("- cmdx is on average %.2fx faster than PyMEL" % avg)
avg = average("cmds", "cmdx", data)
print("- cmdx is on average %.2fx faster than cmds" % avg)