diff options
author | Kevin Rose <kevin+gh@maypark.com> | 2017-08-01 10:34:00 -0500 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2017-08-01 08:34:00 -0700 |
commit | 3282a84e3068d2ff0ded5a683ceea0806da21ed6 (patch) | |
tree | 5ca756f0045bad32e24a6130dfd1985ec86ee9a2 /docs | |
parent | 89a68942acdeeb51ceb102d19153dcc23ba8c0dd (diff) | |
download | flatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.tar.gz |
[Python] (scalar) vector reading speedup via numpy (#4390)
* Add numpy accessor to python flatbuffers scalar vectors
* Update python tests to test numpy vector accessor
* Update appveyor CI to run Python tests, save generated code as artifact
* Update example generated python code
* Add numpy info to python usage docs
* Update test schema and python tests w/ multi-byte vector
* did not mean to push profiling code
* adding float64 numpy tests
Diffstat (limited to 'docs')
-rwxr-xr-x | docs/source/PythonUsage.md | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/docs/source/PythonUsage.md b/docs/source/PythonUsage.md index 2a0cddee..f338cda4 100755 --- a/docs/source/PythonUsage.md +++ b/docs/source/PythonUsage.md @@ -64,6 +64,33 @@ Now you can access values like this: pos = monster.Pos() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +## Support for Numpy arrays + +The Flatbuffers python library also has support for accessing scalar +vectors as numpy arrays. This can be orders of magnitude faster than +iterating over the vector one element at a time, and is particularly +useful when unpacking large nested flatbuffers. The generated code for +a scalar vector will have a method `<vector name>AsNumpy()`. In the +case of the Monster example, you could access the inventory vector +like this: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py} + inventory = monster.InventoryAsNumpy() + # inventory is a numpy array of type np.dtype('uint8') +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +instead of + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py} + inventory = [] + for i in range(monster.InventoryLength()): + inventory.append(int(monster.Inventory(i))) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Numpy is not a requirement. If numpy is not installed on your system, +then attempting to access one of the `*asNumpy()` methods will result +in a `NumpyRequiredForThisFeature` exception. + ## Text Parsing There currently is no support for parsing text (Schema's and JSON) directly |