simphony / simphony-osp

A framework that aims to achieve interoperability between software such as simulation engines, databases and data repositories using a knowledge graph as the common language.
https://simphony.readthedocs.io
Other
16 stars 12 forks source link

Cache access to namespace elements to improve performance #764

Closed kysrpex closed 2 years ago

kysrpex commented 2 years ago

Simple fix to the performance problem explained below.


Consider the following code snippets, labeled as slow code and fast code.

Slow code

from osp.core.namespaces import mechanical as mt
from osp.core.namespaces import math, metrology
from osp.core.namespaces import properties as prop
from time import time
from tqdm import tqdm

n = (333636 // 3)

start = time()
for i in tqdm(range(0, n)):
    bending_stiffness_average_value = 0.0312  # some numerical value
    cableSystem = mt.CableBundle()
    bendingStiffness_average = mt.BendingStiffness()
    realNumber = math.Real(hasNumericalData=bending_stiffness_average_value)
    bendingStiffness_average.add(realNumber, rel=metrology.hasQuantityValue)
    cableSystem.add(bendingStiffness_average, rel=prop.hasProperty)
end = time()
print("time %.2f" % (end - start))

Fast code

from osp.core.namespaces import mechanical as mt
from osp.core.namespaces import math, metrology
from osp.core.namespaces import properties as prop
from time import time
from tqdm import tqdm

n = (333636 // 3)

start = time()
# Expensive calls below
cable_bundle_class = mt.CableBundle
bending_stiffness_class = mt.BendingStiffness
real_class = math.Real
rel1 = metrology.hasQuantityValue
rel2 = prop.hasProperty
# Expensive calls above
for i in tqdm(range(0, n)):
    bending_stiffness_average_value = 0.0312  # some numerical value
    cableSystem = cable_bundle_class()
    bendingStiffness_average = bending_stiffness_class()
    realNumber = real_class(hasNumericalData=bending_stiffness_average_value)
    bendingStiffness_average.add(realNumber, rel=rel1)
    cableSystem.add(bendingStiffness_average, rel=rel2)
end = time()
print("time %.2f" % (end - start))

Estimation given by tqdm for each code snippet without this fix:

Estimation given by tqdm for each code snippet with this fix: