Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache access to namespace elements to improve performance #764

Merged
merged 3 commits into from
Apr 5, 2022

Conversation

kysrpex
Copy link
Contributor

@kysrpex kysrpex commented Mar 31, 2022

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:

  • slow code: 134/111212 [00:14<3:24:00, 9.07it/s]
  • fast code: 2117/111212 [00:09<07:46, 234.05it/s]

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

  • slow code: 2099/111212 [00:07<06:39, 272.89it/s]
  • fast code: 2130/111212 [00:07<06:35, 276.00it/s]

@kysrpex kysrpex added 📈 performance 🔨 simple fix Likely to be solvable in at most a few hours (full-time). labels Mar 31, 2022
@kysrpex kysrpex self-assigned this Mar 31, 2022
@kysrpex kysrpex marked this pull request as ready for review March 31, 2022 10:36
@kysrpex kysrpex merged commit 5e0dc92 into dev Apr 5, 2022
@kysrpex kysrpex deleted the performance/labels branch April 5, 2022 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📈 performance 🔨 simple fix Likely to be solvable in at most a few hours (full-time).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants