Skip to content

Instantly share code, notes, and snippets.

@typoman
Last active September 8, 2020 10:09
Show Gist options
  • Save typoman/38a41811ad5dd8eb16a9d8acb6519a7f to your computer and use it in GitHub Desktop.
Save typoman/38a41811ad5dd8eb16a9d8acb6519a7f to your computer and use it in GitHub Desktop.
This can be used to find faster ways to perform tasks in RoboFont using python
import time
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
print('%r %2.2f ms' %(method.__name__, (te - ts) * 1000))
return result
return timed
f = CurrentFont()
# -----------------------------------------------------------------------------
# 1. Check if glyph is in a font
@timeit
def testGlyphInFont(f):
for i in range(1000):
'a' in f
glyphSet = set(f.keys())
@timeit
def testGlyphInSet(glyphSet):
for i in range(1000):
'a' in glyphSet
testGlyphInFont(f)
testGlyphInSet(glyphSet)
# Result: Using a set is a lot faster than using Font for checking if a glyph exist
# -----------------------------------------------------------------------------
# 1. Check index of a glyph name in glyphOrder
@timeit
def testFontGlyphOrder(f):
for i in range(100):
f.glyphOrder.index('a')
glyphOrder = f.glyphOrder[:]
@timeit
def testListGlyphOrder(glyphOrder):
for i in range(100):
glyphOrder.index('a')
testFontGlyphOrder(f)
testListGlyphOrder(glyphOrder)
# Result: Caching glyphOrder inside a tuple is a lot faster for operations inside a loop compared to using Font.glyphOrder property
# -----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment