Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Timeit in C4D

    Scheduled Pinned Locked Moved PYTHON Development
    3 Posts 0 Posters 302 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        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

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post