Timeit in C4D
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 14/02/2012 at 13:11, xxxxxxxx wrote:
I've been playing around with the timeit module. And it works fine:
import c4d import timeit def main() : print timeit.timeit('x = 10; y = 100; z = x ** y') if __name__=='__main__': main()
But I'm having a problem getting it to work with C4D methods:
import c4d import timeit def method1(x,y) : z = zip(x,y) z.sort() return zip(*z) def main() : people = ['Jim', 'Pam', 'Micheal', 'Dwight'] ages = [27, 25, 4, 9] print method1(people, ages) t = timeit.Timer(setup='from __main__ import method1', stmt='method1()') print t.timeit() #<-----------Error: Cannot import name method1 if __name__=='__main__': main()
I think this is the part that is wrong: setup='from __main__ import method1'
Anyone know the correct syntax needed here?-ScottA
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 15/02/2012 at 05:02, xxxxxxxx wrote:
I'd guess the script that is executed in the manager is not set as being the __main__ module internally. A workaround might be something like this, but not a quite nice-one.
import sys import c4d import timeit def method1(x ,y) : z = zip(x, y) z.sort() return zip(*z) def main() : people = ['Jim', 'Pam', 'Micheal', 'Dwight'] ages = [27, 25, 4, 9] print method1(people, ages) # create an objects that holds our scope as attributes # and add it to the loaded modules scope = dict(people = people, ages = ages, method1 = lambda self, x, y: method1(x, y)) sys.modules['__fakemod__'] = type('', (), scope)() # make the timing stuff t = timeit.Timer('method1(people, ages)', 'from __fakemod__ import method1, people, ages') print t.timeit(number = 1000) # remove the fake-module del sys.modules['__fakemod__'] main()
Edit: That one might be more scalable.
Cheers,
-Niklas -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 15/02/2012 at 08:13, xxxxxxxx wrote:
Thanks Niklas.
That's pretty convoluted stuff just to get it to work. But good stuff to learn from.-ScottA