@@ -57,6 +57,8 @@ and used in various methods.
5757
5858This is covered in further detail :ref: `Controlling garbage collection <controlling_gc >` below.
5959
60+ .. _speed_buffers :
61+
6062Buffers
6163~~~~~~~
6264
@@ -69,6 +71,13 @@ example, objects which support stream interface (e.g., file or UART) provide ``r
6971method which allocates new buffer for read data, but also a ``readinto() `` method
7072to read data into an existing buffer.
7173
74+ Some useful classes for creating reusable buffer objects:
75+
76+ - :class: `bytearray `
77+ - :mod: `array ` (:ref: `discussed below<speed_arrays> `)
78+ - :class: `io.StringIO ` and :class: `io.BytesIO `
79+ - :class: `micropython.RingIO `
80+
7281Floating point
7382~~~~~~~~~~~~~~
7483
@@ -80,15 +89,20 @@ point to sections of the code where performance is not paramount. For example,
8089capture ADC readings as integers values to an array in one quick go, and only then
8190convert them to floating-point numbers for signal processing.
8291
92+ .. _speed_arrays :
93+
8394Arrays
8495~~~~~~
8596
8697Consider the use of the various types of array classes as an alternative to lists.
87- The `array ` module supports various element types with 8-bit elements supported
98+ The :mod: `array ` module supports various element types with 8-bit elements supported
8899by Python's built in `bytes ` and `bytearray ` classes. These data structures all store
89100elements in contiguous memory locations. Once again to avoid memory allocation in critical
90101code these should be pre-allocated and passed as arguments or as bound objects.
91102
103+ Memoryviews
104+ ~~~~~~~~~~~
105+
92106When passing slices of objects such as `bytearray ` instances, Python creates
93107a copy which involves allocation of the size proportional to the size of slice.
94108This can be alleviated using a `memoryview ` object. The `memoryview ` itself
@@ -118,6 +132,23 @@ of buffer and fills in entire buffer. What if you need to put data in the
118132middle of existing buffer? Just create a memoryview into the needed section
119133of buffer and pass it to ``readinto() ``.
120134
135+ Strings vs Bytes
136+ ~~~~~~~~~~~~~~~~
137+
138+ MicroPython uses :ref: `string interning <qstr >` to save space when there are
139+ multiple identical strings. Each time a new string is allocated at runtime (for
140+ example, when two other strings are concatenated), MicroPython checks whether
141+ the new string can be interned to save RAM.
142+
143+ If you have code which performs performance-critical string operations then
144+ consider using :class: `bytes ` objects and literals (i.e. ``b"abc" ``). This skips
145+ the interning check, and can be several times faster than performing the same
146+ operations with string objects.
147+
148+ .. note :: The fastest performance will always be achieved by avoiding new object
149+ creation entirely, for example with a reusable :ref: `buffer as described
150+ above<speed_buffers>`.
151+
121152Identifying the slowest section of code
122153---------------------------------------
123154
0 commit comments