Vague Python script performance question

I’m working on converting a VBscript into Python, to learn Python and hopefully leverage some RhinoCommon stuff to speed it up. The script is for fitting (very specific) curves to other curves, using a brute force randomly-try-anything-then-check-accuracy approach. I’ve left the VBscript version to run for a day, churning through millions of possibilities, only interacting with the document to draw in a new best-fit when found. The Python version right now doesn’t even draw every best-fit, just after completing a set of options, so I kinda presumed that even if it was still mostly using rhinoscript functions it would be faster, but it behaves kinda weird.

Less than a minute into running the script Rhino stops displaying the debugging messages I’m printing to the command line and becomes unresponsive, to the point where if(as I would do with the VBScript version) I can’t open up a second session of the same file. Eventually it finishes running the script and spits out the rest of the command line messages and all the output at once, which is obviously not how it’s set up to work. Is there something known about quirks of this stuff that might explain that?

Hi Jim, I think I read here about some python print quirks in tight loops.

Are you sure that there is no way for an “optimizer”? I made some nice “solvers” with python and the speed can be impressive.

That’s what it looks like.

Math has never been my strongest suit, so I would guess so. I discovered a few stock Python features already that would make a more ‘systematic’ approach simpler to code.


you might try this to see if it prints any better:


To make Rhino respond to ESC key during a tight loop, add an escape_test to the first line of the loop:

import scriptcontext

def DoSomething():
    for i in xrange(10000):
        if scriptcontext.escape_test(False): return
        print i


Thanks, that does help.


I have noticed when I have a script that is for example adding a large number of lines to the document, rhino will add them consecutively up to a point, then almost as though it realises it is taking too long, stops redrawing the screen after every loop, lets the script run then adds all the lines at the end.

Largely this seems to be efficient. Interacting with the active document takes time that can drastically slow down even simple loops and you are better off not adding objects to the document until the end.

If you really want to see all the lines as they are computed you can add “rs.redraw()” to force a redraw.


that is inevitable if you use for example rs.AddLine(), as it includes a redraw after every addition. So even when you disable redraw, it will slow down, as it redraws all views after every line addition. One way to get around it is to use RhinoCommon instead to add the lines, eg.:

import Rhino
import scriptcontext
import random, math

def AddRandomLines():
    for i in xrange(1000):
        x = 10*math.cos(random.randint(1,360))
        y = 10*math.sin(random.randint(1,360))
        pt = Rhino.Geometry.Point3d(x, y, 0)
        origin = Rhino.Geometry.Point3d(0,0,0)
        line = Rhino.Geometry.Line(origin, pt)

…then use scriptcontext.doc.Views.Redraw() at the end of the script to redraw once.