What's the lowest latency way to read AIN?

I’ve got my hardware all set and working, but I’m getting a pretty horrible lag behavior when I cat AIN files and I don’t fully understand why. My testing set up is a cheapo IR range sensor connected to AIN5. I can distinguish between “point blank” and “out of range” very reliably, but when I transition between the two it reports the old value the first time I cat the file:

cat AIN5 (with sensor uncovered, pointed out of range)

1795

cat AIN5 (with finger covering the sensor)

1795

cat AIN5 (with finger still covering the sensor)

232

cat AIN5 (with sensor uncovered, pointing out of range)

219

cat AIN5 (with sensor still uncovered)

1794

cat AIN5 (with sensor still uncovered)

1795

Any idea why this is happening? I can reproduce the same behavior using Adafruit’s Python library.

More to the point, if I’m going to be using the AIN as the input to a control loop, what’s the lowest latency way to do so? Is there any way to avoid having to reopen a file descriptor each time? I’d much rather just open a stream and go.

–M@