@@ -57,6 +57,8 @@ and used in various methods.
57
57
58
58
This is covered in further detail :ref: `Controlling garbage collection <controlling_gc >` below.
59
59
60
+ .. _speed_buffers :
61
+
60
62
Buffers
61
63
~~~~~~~
62
64
@@ -69,6 +71,13 @@ example, objects which support stream interface (e.g., file or UART) provide ``r
69
71
method which allocates new buffer for read data, but also a ``readinto() `` method
70
72
to read data into an existing buffer.
71
73
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
+
72
81
Floating point
73
82
~~~~~~~~~~~~~~
74
83
@@ -80,15 +89,20 @@ point to sections of the code where performance is not paramount. For example,
80
89
capture ADC readings as integers values to an array in one quick go, and only then
81
90
convert them to floating-point numbers for signal processing.
82
91
92
+ .. _speed_arrays :
93
+
83
94
Arrays
84
95
~~~~~~
85
96
86
97
Consider 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
88
99
by Python's built in `bytes ` and `bytearray ` classes. These data structures all store
89
100
elements in contiguous memory locations. Once again to avoid memory allocation in critical
90
101
code these should be pre-allocated and passed as arguments or as bound objects.
91
102
103
+ Memoryviews
104
+ ~~~~~~~~~~~
105
+
92
106
When passing slices of objects such as `bytearray ` instances, Python creates
93
107
a copy which involves allocation of the size proportional to the size of slice.
94
108
This 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
118
132
middle of existing buffer? Just create a memoryview into the needed section
119
133
of buffer and pass it to ``readinto() ``.
120
134
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
+
121
152
Identifying the slowest section of code
122
153
---------------------------------------
123
154
0 commit comments