Hey Jo,
Nice job ! but my approach did not involve one function approach to
speed up as you used in your profiling, rather it was just to globally
store the data and look up as needed. The initial data pull was still
the same as Jeremy with a little correction from Matt, but after that
the subsequent function calls do not need to query data again as it is
already available through the hash. So , my approach was really to store
the data once, which I think is working good for Jeremy for now.
Cheers!!
Alok.
On 27/07/2012 7:25 PM, jo benayoun wrote:
Hey Jeremie,
Here is what I am coming with on a character with thousands of
expressions:
# method 1 took 5.42011094882 and find 3 expressions.
# method 2 took 2.31037755754 and find 3 expressions.
# method 3 took 5.41616293425 and find 3 expressions.
I would recommend you to take a look at a paradigm that is called
Data-Oriented Design and which is specially considered to avoid CPU
usage and fast computation.
Let me know if this fit your needs :)
-- Jo
import time
SI_EXPRESSION_ID = 49
def Jerems( param ):
allExpressions = Application.FindObjects(None,
"{12723EB2-7DDB-11D0-A57F-00A0C91412DE}")
result = []
for exp in allExpressions:
if exp.OutputPorts.Count:
for port in exp.InputPorts:
if port.Target2.IsEqualTo(param):
result.append(exp)
break
return result
def exprs_from_parameters(parameters):
if type(parameters) not in (list, tuple):
parameters = (parameters, )
res = list()
parameters = [p for p in parameters if p is not None]
nparameters = len(parameters)
if nparameters == 0:
return res
expressions = Application.FindObjects2(SI_EXPRESSION_ID)
res = [list() for i in xrange(nparameters)]
for exp in expressions:
sources = [iport.Target2 for iport in exp.InputPorts]
for i in xrange(nparameters):
b = [src.IsEqualTo(parameters[i]) for src in sources]
if any(b):
res[i].append(exp)
return tuple(res)
def Aloks(param):
PARAM_EXPR_DICT = {}
for expr in Application.FindObjects(None,
"{12723EB2-7DDB-11D0-A57F-00A0C91412DE}"):
ports = expr.InputPorts
if not ports.Count:
continue
for port in ports:
paramName = port.Target2.FullName
if not PARAM_EXPR_DICT.has_key(paramName):
PARAM_EXPR_DICT[paramName] = []
PARAM_EXPR_DICT[paramName].append(expr)
return PARAM_EXPR_DICT[param.FullName] if
PARAM_EXPR_DICT.has_key(param.Name) else None
msg = "method {0} took {1} and find {2} expressions."
def timethat1():
ppg = Application.Selection(0)
res = []
t = time.clock()
for param in ppg.Parameters:
res.append(Jerems(param))
t = time.clock() - t
print msg.format(1, t, sum([len(r) for r in res]))
def timethat2():
ppg = Application.Selection(0)
t = time.clock()
res = exprs_from_parameters(tuple(ppg.Parameters))
t = time.clock() - t
print msg.format(2, t, sum([len(r) for r in res]))
def timethat3():
ppg = Application.Selection(0)
res = []
t = time.clock()
for param in ppg.Parameters:
res.append(Aloks(param))
t = time.clock() - t
print msg.format(3, t, sum([len(r) for r in res]))
timethat1()
timethat2()
timethat3()
2012/7/27 Jeremie Passerin <[email protected]
<mailto:[email protected]>>
Thanks Matt !
Interesting solution Alok, my issue is not the 2sec that it takes
to perform the seach but that I am looping on this method about 20
times... so it makes sense to try something like that ! I'll give
it a try right now !
On 27 July 2012 15:54, Alok <[email protected]
<mailto:[email protected]>> wrote:
Maybe globally store the expressions with their params in a
hash (dictionary in Python) and then do the reverse look up.
Will not run faster but will save you time for each call to
getExpressionsDrivenByParameter()
In this case:
PARAM_EXPR_DICT = {}
for expr in Application.FindObjects(None,
"{12723EB2-7DDB-11D0-A57F-00A0C91412DE}"):
ports = expr.InputPorts
if not ports.Count:
continue
for port in in ports :
paramName = port.Target2.FullName
if not PARAM_EXPR_DICT.has_key(paramName):
PARAM_EXPR_DICT[paramName] = []
PARAM_EXPR_DICT[paramName].append(expr)
Then you can test it for parameter simply by:
def getExpressionsDrivenByParameter( param ):
return (PARAM_EXPR_DICT[param.FullName] if
PARAM_EXPR_DICT.has_key(param.Name) else None)
Alok.
On 27/07/2012 5:44 PM, Jeremie Passerin wrote:
Hi guys,
I was wondering what is your technique to get all the
expressions driven by a parameter.
I'm trying to find the fastest way to do it.
here is my code :
def getExpressionsDrivenByParameter( param ):
allExpressions = xsi.FindObjects(None,
"{12723EB2-7DDB-11D0-A57F-00A0C91412DE}")
result = []
for exp in allExpressions:
for port in exp.InputPorts:
if port.Target2.IsEqualTo(param) and exp.OutputPorts.Count:
result.append(exp)
break
return result
With around 5000 expressions in my scene it takes about 2sec
to get the result. Anyone got a faster solution ?
cheers,
Jeremie
No virus found in this message.
Checked by AVG - www.avg.com <http://www.avg.com>
Version: 2012.0.2197 / Virus Database: 2437/5158 - Release
Date: 07/27/12
No virus found in this message.
Checked by AVG - www.avg.com <http://www.avg.com>
Version: 2012.0.2197 / Virus Database: 2437/5158 - Release Date: 07/27/12