Import pyrevit vs import Autodesk

I’ve noticed that when I import pyrevit as opposed to importing Autodesk directly, I get a performance hit of a few seconds. For example, lets say I have a simple script where I only need Autodesk.Revit.DB imported. If I import as “import Autodesk.Revit.DB as DB”, the script runs very quickly. But if I import as “from pyrevit import DB”, the script takes a few seconds before executing, presumably because of all the stuff it has to load from pyrevit.

Is there a way around the lag? is there a better way to import pyrevit? there are some things importing pyrevit is invaluable for (forms, script), but the slowness is a bummer

Just a little test example:

This code runs in 19.5 seconds:

import time
st = time.time()
from pyrevit import revit, DB
doc = revit.doc

worksets = [
    'ARCH - Core and Shell',
    'ARCH - Interior Buildout',
    'ARCH - RCP',
    'ARCH - Furniture',
    'ARCH - Render',
    'REVIT - MEP',
    'REVIT - Structural',
    'CAD - Survey',
    'CAD - Landscape'
    ]

if not doc.IsWorkshared:
    doc.EnableWorksharing("Shared Levels and Grids", "Workset1")

t = DB.Transaction(doc, 'Create Worksets')
t.Start()
for w in worksets:
    DB.Workset.Create(doc, w)
t.Commit()

print time.time() - st

and this code takes 16.1 seconds:

import time
st = time.time()
import Autodesk.Revit.DB as DB
doc = __revit__.ActiveUIDocument.Document

worksets = [
    'ARCH - Core and Shell',
    'ARCH - Interior Buildout',
    'ARCH - RCP',
    'ARCH - Furniture',
    'ARCH - Render',
    'REVIT - MEP',
    'REVIT - Structural',
    'CAD - Survey',
    'CAD - Landscape'
    ]

if not doc.IsWorkshared:
    doc.EnableWorksharing("Shared Levels and Grids", "Workset1")

t = DB.Transaction(doc, 'Create Worksets')
t.Start()
for w in worksets:
    DB.Workset.Create(doc, w)
t.Commit()

print time.time() - st

A few seconds generally isn’t a big deal and when I don’t need ‘forms’ or ‘script’, I will probably avoid importing pyrevit, but I’m just curious if there is something I could do differently. Thanks!

Hi, I’m experiencing the same performance slowness with “from pyrevit import forms, script”
Sometimes the cursor is spinning for around 10 seconds before running the first line of actual code. Does anyone know what the problem is or experience the same thing?

I have run into something similar. I have a bit of code (see bottom) where I simply run from DB and I get drastically different run times than importing from pyrevit

Times are right after import and setting up doc/uidoc variables, then at the end of the transaction - Note that the tests are taken after closing and re opening Revit (2022.1 for these tests)

DB import:
0.01596 & 1.1934 (for first run)
0.01599 & 0.2025 (for second run - probably faster due to worksharing not having to check the element out again)

pyrevit import:
1.5015 & 2.7338 (first run)
1.5989 & 1.9700 (second run)

Another weird thing I’ve seen is that the pyrevit imports keep getting longer where DB does not…

1.7228 & 2.0854 (3rd)
1.8115 & 2.1605 (4th)

2.4808 & 2.6745 (10th)

3.5544 & 3.9016 (20th)

I agree that for the most part, we haven’t seen an issue with importing pyrevit, but I noticed the script was sluggish as I was making it. I wasn’t expecting a script to take upwards of 7 seconds to simply rotate an element. We were hoping to make a dockable pane with some scripts like this to do some quick things that people were needing multiple steps for (for this - creating a section/moving it around and using the rotate tool to get something to rotate)

Now that I think about it, might be a good thing to put into an issue on GitHub?

DB only:

import time
st = time.time()
from math import radians
import Autodesk.Revit.DB as DB

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

print(time.time() - st)
t = DB.Transaction(doc, "Rotate Element (Y-Axis)")
t.Start()
selection = uidoc.Selection.GetElementIds()
# >>>>>>>>>> GET CENTER POINT
elem = doc.GetElement(selection[0])
bounding_box = elem.get_BoundingBox(doc.ActiveView)
point = (bounding_box.Min + bounding_box.Max) / 2

# >>>>>>>>>> AXIS LINE
axis_line = DB.Line.CreateBound(point, point + DB.XYZ.BasisY)
# >>>>>>>>>> ROTATE
DB.ElementTransformUtils.RotateElement(doc, elem.Id, axis_line, radians(90))
t.Commit()
print(time.time() - st)

pyRevit only

import time
st = time.time()
from pyrevit import HOST_APP
from pyrevit.revit import Transaction
from pyrevit import DB
from math import radians


doc = HOST_APP.doc
uidoc = HOST_APP.uidoc

print(time.time() - st)
with Transaction("Rotate Element (Y-Axis)"):
	selection = uidoc.Selection.GetElementIds()
	# >>>>>>>>>> GET CENTER POINT
	elem = doc.GetElement(selection[0])
	bounding_box = elem.get_BoundingBox(doc.ActiveView)
	point = (bounding_box.Min + bounding_box.Max) / 2

	# >>>>>>>>>> AXIS LINE
	axis_line = DB.Line.CreateBound(point, point + DB.XYZ.BasisY)
	# >>>>>>>>>> ROTATE
	DB.ElementTransformUtils.RotateElement(doc, elem.Id, axis_line, radians(90))
print(time.time() - st)
1 Like