Python Forum

Full Version: Time multiple functions within functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using the timeit function how can I time the different small functions that I reuse in my main function?
So I am not looking for how long my whole script is taking but more how long the different components / small functions take?
It looks like homework, what have you tried?
Well I am working with IfcOpenshell and Opencascade and some of my functions are running inefficiently / too long.
Here is an excerpt of my script:

def listofProduct_shapes(ifc_file):
    products = ifc_file.by_type("IfcProduct")
    product_shapes = []
    for product in products:
        if product.is_a("IfcOpeningElement") or product.is_a("IfcSite") or product.is_a("IfcAnnotation"): continue
        else:
            try:
                shape = ifcopenshell.geom.create_shape(settings, product).geometry
                product_shapes.append((product, shape))
            except:
                continue
    return product_shapes

# Function to display the shapes of the file
def displayProduct_shapes(ifc_file):
    products = ifc_file.by_type("IfcProduct")
    product_shapes = []
    for product in products:
        if product.is_a("IfcOpeningElement") or product.is_a("IfcSite") or product.is_a("IfcAnnotation"): continue
        try:
            shape = ifcopenshell.geom.create_shape(settings, product).geometry
            product_shapes.append((product, shape))
        except:
            continue
        ifcopenshell.geom.utils.display_shape(shape)
        occ_display.FitAll()

# Bounding box functions
def boundingBox_center(ifc_file): #Outputs the bounding box center coordinates
    walls = ifc_file.by_type("IfcWall")

    wall_shapes = []
    bbox = OCC.Bnd.Bnd_Box()
    for wall in walls:
        shape = ifcopenshell.geom.create_shape(settings, wall).geometry

        wall_shapes.append((wall, shape))
        OCC.BRepBndLib.brepbndlib_Add(shape, bbox)
    bounding_box_center = ifcopenshell.geom.utils.get_bounding_box_center(bbox)
    return bounding_box_center

def boundingBox_coordinates(ifc_file): # Function that outputs the outer coordinates of the objects bounding box
    walls = ifc_file.by_type("IfcWall")

    wall_shapes = []
    bbox = OCC.Bnd.Bnd_Box()
    for wall in walls: #walls are used as the outer element
        shape = ifcopenshell.geom.create_shape(settings, wall).geometry

        wall_shapes.append((wall, shape))
        OCC.BRepBndLib.brepbndlib_Add(shape, bbox)
    return bbox.Get()


def calc_surfaceArea_face(wire, face):
    wire_data = OCC.ShapeExtend.ShapeExtend_WireData(wire, True, True)
    wire_data_handle = OCC.ShapeExtend.Handle_ShapeExtend_WireData(wire_data)
    # The surface area of the face is calculated and appended to the list
    surface_area = abs(OCC.ShapeAnalysis.shapeanalysis_TotCross2D(wire_data_handle, face))
    # if surface_area < 0.01:
    #     surface_area = 0
    return surface_area
So my question is. Can insert a Time module inside every function to see how long it takes to run when I call it in my main function?

Thanks for the help
Why not simply profile the code with the profile module to know how much time is spent in each function?
Thank you very much that worked!