You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
setResolution() and other functions looping by index exhibit O(n^2) duration.
They also send DS18 commands to every device on the bus, even those that are not DS18, which wastes time and may do something unexpected in consequence.
Steps To Reproduce Problem
Inspect the code.
Looping over all devices using for(i=0; i<devices; i++) { ... getAddress(); ...} is inherently inefficient because getAddress() carries out a linear search of the bus from the start for every call. This leads to O(n^2) behaviour.
Many functions are also dangerous because they apply DS18 commands to every device on the bus even those that are unsupported and may do something unexpected in consequence.
The fix is to provide an iterator, similar to that provided by OneWire, which iterates over only supported devices. It is both more efficient and cleaner code.
// start iterator over the supported devices
void DallasTemperature::iteratorReset() {
_wire->reset_search();
}
// continue iteration over the supported devices
bool DallasTemperature::iteratorNext(uint8_t* deviceAddress) {
while (_wire->search(deviceAddress)) {
if (validAddress(deviceAddress) && validFamily(deviceAddress))
return true;
}
return false;
}
The loop in setResolution() and similarly for similar loops in other functions, then becomes
iteratorReset();
while (iteratorNext(address)) {
setResolution(address, bitResolution, true);
}
NB the above has not yet been tested as I am engaged in some other more drastic changes. More on that in due course.
The text was updated successfully, but these errors were encountered:
As mentioned in milesburton#193 the issue stems from repeated usage of getAddress(). It doesn't seem to me to be necessary to use iterators to sort out the performance issue in the library, just a general avoidance of getAddress() outside index based functions.
Since getAddress() appears to be used to do a bit more than iterate through addresses I added validAddress() as an additional condition, it might not be needed.
I only found two locations where this appears to happen so milesburton#193 may be resolved entirely by this. General advice to users of the library may be to avoid index based functions inside loops. This might be where an "iterator" interface may be useful inside the class, such that performing functions across devices remains somewhat linear.
setResolution() and other functions looping by index exhibit O(n^2) duration.
They also send DS18 commands to every device on the bus, even those that are not DS18, which wastes time and may do something unexpected in consequence.
Steps To Reproduce Problem
Inspect the code.
Looping over all devices using
for(i=0; i<devices; i++) { ... getAddress(); ...}
is inherently inefficient because getAddress() carries out a linear search of the bus from the start for every call. This leads to O(n^2) behaviour.Many functions are also dangerous because they apply DS18 commands to every device on the bus even those that are unsupported and may do something unexpected in consequence.
The fix is to provide an iterator, similar to that provided by OneWire, which iterates over only supported devices. It is both more efficient and cleaner code.
The loop in setResolution() and similarly for similar loops in other functions, then becomes
NB the above has not yet been tested as I am engaged in some other more drastic changes. More on that in due course.
The text was updated successfully, but these errors were encountered: