Skip to content

Speed optimisations for input()/output() #11

@sheffieldnikki

Description

@sheffieldnikki

Obviously the sysfs isn't ideal if you want to waggle GPIO pins at high speed, but the library can be made a bit faster:

  1. input()/output() are aliases for read()/set(), but the way they are defined _verify() is called twice for each call, once for output() and then again for set(). So change to:
def input(pin):
    '''read the pin. Same as read'''
    return read(pin)

def output(pin, value):
    '''set the pin. Same as set'''
    return set(pin, value)

or just:

input = read
output = set
  1. Wrapping read()/set() with _verify() adds around 20% overhead to those calls. Is it really needed? If it is needed, doing the same "if pin in _open" test inside the set() function has far less overhead

  2. Is there any benefit from defining LOW, HIGH = 'low', 'high' instead of as int? Removing the:

    if value is LOW:
        value = 0
    value = int(bool(value))

from inside read()/set() gives another 5-10% improvement. The RPi.GPIO library uses ints rather than strings for pin values.

  1. The logging call adds around 30% overhead to read()/set() even with no debug output (!) i.e., without "logging.basicConfig(level=logging.DEBUG)" set at the top of the library. Even this would help a lot:
    if (log.level==logging.DEBUG):
        log.debug("Write {0}: {1}".format(pin, value))

All told, those 4 changes doubles the maximum speed of the library, on a modest dual core ARM board.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions