10

I'm trying to find the qickest way to parse sensor data from a smartphone for a realtime application. The Format looks like this:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<NodeId>0</NodeId>
<Accelerometer>
    <Accelerometer1>-.1875240802764893</Accelerometer1>
    <Accelerometer2>4.6734819412231445</Accelerometer2>
    <Accelerometer3>8.312667846679688</Accelerometer3>
</Accelerometer>
<Gyroscope>
    <Gyroscope1>-0.10551923513412476</Gyroscope1>
    <Gyroscope2>0.009592439979314804</Gyroscope2>
    <Gyroscope3>0.019185146316885948</Gyroscope3>
</Gyroscope>
<Gravity>
    <Gravity1>-1.2976515293121338</Gravity1>
    <Gravity2>3.672762393951416</Gravity2>
    <Gravity3>9.003327369689941</Gravity3>
</Gravity>
<TimeStamp>1377767599250</TimeStamp>

The available sensor data might change depending on the phone. But once the connection is established the structure of the packages won't change, so maybe parts of the parsing could be skipped.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
BoshWash
  • 5,320
  • 4
  • 30
  • 50
  • A general suggestion - depending on how fast your I/O is (are you fetching the data over a slow connection?) and how big the file is it may make sense to parse the data as you're transmitting it, i.e. use a SAX parser and feed it the chunks of markup as you receive it. – Frerich Raabe Aug 29 '13 at 10:17
  • Many of the key considerations in selecting a parser are about memory efficiency, as opposed to time efficiency. Your question doesn't speak to that, and the accepted answer only speaks to DOM-style solutions, which inherently are somewhat memory-inefficient. – Charles Duffy Aug 12 '15 at 15:17
  • @FrerichRaabe, ...a parser implementing the SAX API was the old model for streaming, but I'd actually go with lxml.iterparse these days -- still fast and memory-efficient, but much less painful to work with. – Charles Duffy Aug 12 '15 at 15:18

1 Answers1

18

If parsing speed is a key factor for you, consider using cElementTree or lxml.

There are definitely more options out there, see these threads:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195