Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Tips and tricks ➜ Python's issue with callbacks

Python's issue with callbacks

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Ked   Russia  (524 posts)  Bio
Date Tue 26 Aug 2003 04:35 PM (UTC)

Amended on Thu 28 Aug 2003 06:32 AM (UTC) by Nick Gammon

Message
Having recently become completely disgusted with Vbscript, I've gone to testing Python with a purpose of figuring out performance benefits of translating about 2k lines of code into that language. After fidling with a code snippet, found in Python's newsgroup, I arrived at a few trivial conclusions: Python is faster than Vbscript by alot when it comes to math, it has less overhead associated with function calls than Vbscript does, not to mention that it is by far more powerful and flexible than Microsoft's little victim of incest (although that victim did serve me well for awhile). About the only thing at which Python fails misearably when compared to Vbscript is Mushclient's callbacks. Unfortunately, it's 1.5-4.5 times slower than Vbscript when it comes to using callbacks. Although I couldn't figure out a way to make Python outrun Vbscript on callbacks, I can offer a little advice for making sure you squeeze as much out of Python when doing World.Note's, or any other callback routines, as possible. Compare the following two functions:


from time import clock
aggregateTime = 0.0

def Main(name, output, wildcs):
  global aggregateTime
  list = range(100)
  for i in list:
      startTime = clock()           # timing starts here
      lworldEnTrig = world.EnableTrigger
      lworldEnTrig("test_callbacks", 1)
      lworldEnTrig("test_callbacks", 0)
      totalTime = clock() - startTime   # timing ends here
      aggregateTime = aggregateTime + totalTime 
  averageTime = aggregateTime/100
  world.Note("Average time (mapped version): " + str(averageTime))
  aggregateTime = 0.0      


and


from time import clock
aggregateTime = 0.0

def Main(name, output, wildcs):
  global aggregateTime
  list = range(100)
  for i in list:
      startTime = clock()      # timing starts here
      world.EnableTrigger("test_callbacks", 1)
      world.EnableTrigger("test_callbacks", 0)
      totalTime = clock() - startTime   # timing ends here
      aggregateTime = aggregateTime + totalTime 
  averageTime = aggregateTime/100
  world.Note("Average time (mapped version): " + str(averageTime))
  aggregateTime = 0.0      


The former code has
World.EnableTrigger
callback mapped to a local object
lworldEnTrig
. Even though there's one extra instruction in
the first snippet, its'
For
loop executes 1.5-2 times faster than the one in the latter function. Though you'd need a microscope to notice the performance gain in the above example, it can become quite noticable if you are
using lots of callbacks in your code. Besides, you could get into a habbit of mapping callbacks locally in your functions anyway - it'll speed up your script in the long run, and there's no reason for not getting some extra speed at the cost of a few more lines of code and a little bit of memory.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,418 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.