Python Forum
Time multiple functions within functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time multiple functions within functions
#1
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?
Reply
#2
It looks like homework, what have you tried?
Reply
#3
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
Reply
#4
Why not simply profile the code with the profile module to know how much time is spent in each function?
Reply
#5
Thank you very much that worked!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing writable arguments to functions. Assembler 11 923 Jan-15-2024, 11:32 PM
Last Post: sgrey
  partial functions before knowing the values mikisDeWitte 4 596 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Calling functions by making part of their name with variable crouzilles 4 815 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  __name__ and __main__ in functions Mark17 3 726 Oct-12-2023, 01:55 AM
Last Post: deanhystad
  How can i combine these two functions so i only open the file once? cubangt 4 852 Aug-14-2023, 05:04 PM
Last Post: snippsat
  It seems you have to define functions at the top 357mag 7 1,285 May-10-2023, 03:01 PM
Last Post: jefsummers
  Merge two functions to one SamLiu 4 1,120 May-05-2023, 01:36 PM
Last Post: SamLiu
  Can I get some clarification on importing functions from external files. wh33t 3 897 Feb-25-2023, 08:07 PM
Last Post: deanhystad
  Multiple.ui windows showing at the same time when I only want to show at a time eyavuz21 4 1,018 Dec-20-2022, 05:14 AM
Last Post: deanhystad
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 1,508 Aug-05-2022, 07:53 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020