Okay, that's interesting on the follow-up speed-up of whichever you do next. So the system is working appropriately, and any of these methods is equally good, and behind-the-scenes no matter what you do the caching seems to work. So that's all excellent news: there shouldn't be any significant performance difference based on your exact choice of incantation. All good!
So I 100% agree that for simplicity catalog[fieldName] is the way to go. It's easy to read, pythonic (or at least numpythonic) and performs well.
As to your question on whether pre-fetching the key helps, I came up with the following toy example:
import lsst.afw.table as afwTable
|
import numpy as np
|
schema = afwTable.Schema()
|
nCol = 10
|
for i in xrange(10):
|
schema.addField('col%05d' % (i), type=np.float32,doc='column')
|
|
testCat = afwTable.BaseCatalog(schema)
|
nRow = 1000
|
testCat.table.preallocate(nRow)
|
for i in xrange(nRow):
|
rec=testCat.addNew()
|
for j in xrange(nCol):
|
rec['col%05d' % (j)] = j
|
|
def test1():
|
for i in xrange(nRow):
|
for j in xrange(nCol):
|
test = testCat['col%05d' % (j)][i]
|
|
def test2():
|
keys = []
|
for j in xrange(nCol):
|
keys.append(testCat.schema['col%05d' % (j)].asKey())
|
|
for i in xrange(nRow):
|
for j in xrange(nCol):
|
test = testCat[keys[j]][i]
|
|
The point here is that we're doing row-wise and column-wise access of a long table, so you save a bunch of time if you prefetch the keys:
In [14]: %timeit test1()
|
1 loop, best of 3: 296 ms per loop
|
|
In [15]: %timeit test2()
|
10 loops, best of 3: 113 ms per loop
|
But if you're doing column-wise access of the whole table, it definitely won't make any difference.
Okay, that's interesting on the follow-up speed-up of whichever you do next. So the system is working appropriately, and any of these methods is equally good, and behind-the-scenes no matter what you do the caching seems to work. So that's all excellent news: there shouldn't be any significant performance difference based on your exact choice of incantation. All good!
So I 100% agree that for simplicity catalog[fieldName] is the way to go. It's easy to read, pythonic (or at least numpythonic) and performs well.
As to your question on whether pre-fetching the key helps, I came up with the following toy example:
testCat.table.preallocate(nRow)
The point here is that we're doing row-wise and column-wise access of a long table, so you save a bunch of time if you prefetch the keys:
But if you're doing column-wise access of the whole table, it definitely won't make any difference.