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]>
> 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]> 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
>> Version: 2012.0.2197 / Virus Database: 2437/5158 - Release Date: 07/27/12
>>
>>
>>
>