Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
09f5cb4c
"vscode:/vscode.git/clone" did not exist on "ad75a3907915beba16c0f81c0b129a4b98f2f106"
Commit
09f5cb4c
authored
Mar 03, 2011
by
Christopher Bruns
Browse files
Cache some units results for better performance.
parent
5a6ece03
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
2 deletions
+28
-2
wrappers/python/simtk/unit/unit.py
wrappers/python/simtk/unit/unit.py
+28
-2
No files found.
wrappers/python/simtk/unit/unit.py
View file @
09f5cb4c
...
@@ -249,11 +249,17 @@ class Unit(object):
...
@@ -249,11 +249,17 @@ class Unit(object):
units
[
unit
]
=
power
units
[
unit
]
=
power
return
'Unit(%s)'
%
repr
(
units
)
return
'Unit(%s)'
%
repr
(
units
)
# Performance
_is_compatible_cache
=
{}
def
is_compatible
(
self
,
other
):
def
is_compatible
(
self
,
other
):
"""
"""
Returns True if two Units share the same dimension.
Returns True if two Units share the same dimension.
Returns False otherwise.
Returns False otherwise.
"""
"""
if
self
in
Unit
.
_is_compatible_cache
:
if
other
in
Unit
.
_is_compatible_cache
[
self
]:
return
Unit
.
_is_compatible_cache
[
self
][
other
]
if
not
is_unit
(
other
):
if
not
is_unit
(
other
):
if
self
.
is_dimensionless
():
if
self
.
is_dimensionless
():
return
True
return
True
...
@@ -266,18 +272,32 @@ class Unit(object):
...
@@ -266,18 +272,32 @@ class Unit(object):
for
dimension
,
exponent
in
other
.
iter_base_dimensions
():
for
dimension
,
exponent
in
other
.
iter_base_dimensions
():
other_dims
[
dimension
]
=
exponent
other_dims
[
dimension
]
=
exponent
if
len
(
self_dims
)
!=
len
(
other_dims
):
if
len
(
self_dims
)
!=
len
(
other_dims
):
return
False
result
=
False
return
self_dims
==
other_dims
else
:
result
=
(
self_dims
==
other_dims
)
if
not
self
in
Unit
.
_is_compatible_cache
:
Unit
.
_is_compatible_cache
[
self
]
=
{}
Unit
.
_is_compatible_cache
[
self
][
other
]
=
result
return
result
_is_dimensionless_cache
=
{}
def
is_dimensionless
(
self
):
def
is_dimensionless
(
self
):
"""Returns True if this Unit has no dimensions.
"""Returns True if this Unit has no dimensions.
Returns False otherwise.
Returns False otherwise.
"""
"""
if
self
in
Unit
.
_is_dimensionless_cache
:
return
Unit
.
_is_dimensionless_cache
[
self
]
for
dimension
,
exponent
in
self
.
iter_base_dimensions
():
for
dimension
,
exponent
in
self
.
iter_base_dimensions
():
if
exponent
!=
0
:
if
exponent
!=
0
:
Unit
.
_is_dimensionless_cache
[
self
]
=
False
return
False
return
False
Unit
.
_is_dimensionless_cache
[
self
]
=
True
return
True
return
True
# Performance
_conversion_factor_cache
=
{}
def
conversion_factor_to
(
self
,
other
):
def
conversion_factor_to
(
self
,
other
):
"""
"""
Returns conversion factor for computing all of the common dimensions
Returns conversion factor for computing all of the common dimensions
...
@@ -292,6 +312,9 @@ class Unit(object):
...
@@ -292,6 +312,9 @@ class Unit(object):
factor
=
1.0
factor
=
1.0
if
(
self
is
other
):
if
(
self
is
other
):
return
factor
return
factor
if
self
in
Unit
.
_conversion_factor_cache
:
if
other
in
Unit
.
_conversion_factor_cache
[
self
]:
return
Unit
.
_conversion_factor_cache
[
self
][
other
]
assert
self
.
is_compatible
(
other
)
assert
self
.
is_compatible
(
other
)
factor
*=
self
.
get_conversion_factor_to_base_units
()
factor
*=
self
.
get_conversion_factor_to_base_units
()
factor
/=
other
.
get_conversion_factor_to_base_units
()
factor
/=
other
.
get_conversion_factor_to_base_units
()
...
@@ -312,6 +335,9 @@ class Unit(object):
...
@@ -312,6 +335,9 @@ class Unit(object):
factor
/=
unit
.
conversion_factor_to
(
canonical_units
[
d
])
**
power
factor
/=
unit
.
conversion_factor_to
(
canonical_units
[
d
])
**
power
else
:
else
:
canonical_units
[
d
]
=
unit
canonical_units
[
d
]
=
unit
if
not
self
in
Unit
.
_conversion_factor_cache
:
Unit
.
_conversion_factor_cache
[
self
]
=
{}
Unit
.
_conversion_factor_cache
[
self
][
other
]
=
factor
return
factor
return
factor
def
in_unit_system
(
self
,
system
):
def
in_unit_system
(
self
,
system
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment