Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65405

Re: Logging data from Arduino using PySerial

References <e3b195f8-5a17-4536-9926-2b2ab193719c@googlegroups.com> <mailman.6376.1391489248.18130.python-list@python.org> <b21f1406-a294-4e70-b039-09024e7928e0@googlegroups.com>
Date 2014-02-04 16:18 +1100
Subject Re: Logging data from Arduino using PySerial
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6378.1391491099.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Feb 4, 2014 at 3:57 PM, Thomas <t.tchorzewski@gmail.com> wrote:
> Wow...Thanks Chris! I really appreciate your suggestions (including the stylistic ones). I'll definitely be revising my code as soon as I find the time. As far as profiling goes, I've used timeit in the past but it's quite a pain going through any program block by block. I wish there were a program in which you could just toss in a script and it would spit out the bottlenecks in your code (with suggested performance improvements perhaps)...
>

Well, timeit is good for microbenchmarking. (How useful
microbenchmarking itself is, now, that's a separate question.) For
anything where you can "feel" the program's time by human, it's easy
enough to use the time.time() function.

Add a little helper like this (untested):

last_time = time.time()
def tt(desc):
    global last_time
    cur_time=time.time()
    print("%s: %f"%(desc,cur_time-last_time))
    last_time=cur_time


Then put this all through your code:

....

    # Calculating the length of data to collect based on the
    # sample time and simulation time (set by user)
    max_length = sim_time/sample_time

    tt("Init")

    # Collecting the data from the serial port
    while True:
        data_log.append(connection.readline())
        if len(data_log) > max_length - 1:
            break

    tt("Serial")

...

etc etc. Give it a short description saying what's just happened
(because it'll give the time since the previous timepoint), and then
just eyeball the results to see where the biggest numbers are. If you
do it right, you'll find a whole pile of sections with tiny numbers,
which you can easily ignore, and just a handful that even register.
Then you dig into those sections and see where the slowness is.

Be careful, though. You can easily waste hundreds of expensive dev
hours trying to track down an insignificant time delay. :)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Logging data from Arduino using PySerial Thomas <t.tchorzewski@gmail.com> - 2014-02-03 20:07 -0800
  Re: Logging data from Arduino using PySerial Chris Angelico <rosuav@gmail.com> - 2014-02-04 15:47 +1100
    Re: Logging data from Arduino using PySerial Thomas <t.tchorzewski@gmail.com> - 2014-02-03 20:57 -0800
      Re: Logging data from Arduino using PySerial Chris Angelico <rosuav@gmail.com> - 2014-02-04 16:18 +1100
  Re: Logging data from Arduino using PySerial Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-04 08:56 -0500
  Re: Logging data from Arduino using PySerial MRAB <python@mrabarnett.plus.com> - 2014-02-04 14:05 +0000

csiph-web