1

I have been trying to find the timing of following function that is used to find the address for the ZigBee module connected on my serial port. But when I tried to find the timing using timeit.Timer() method. It is not responding back the time required for execution.

from xbee import ZigBee
from timeit import TImer
import serial

def ByteToHex(bytestring):
    return ''.join(["%02X" % ord(x) for x in bytestring]).strip()

def self_add_tx():
    s = serial.Serial('COM12', 9600) 
    xb = ZigBee(s)
    xb.send('at',
            command='SH')                      
    self_frame_h = xb.wait_read_frame()
    self_addh = self_frame_h['parameter']

    xb.send('at',
            command='SL')                      
    self_frame_l = xb.wait_read_frame()
    s.close()
    self_addl = self_frame_l['parameter']
    self_add = self_addh + self_addl            
    return ByteToHex(self_add)

print Timer(self_add_tx()).timeit()

When I execute the program, it is giving me following error:

Traceback (most recent call last):
  File "I:/HUB/Python_learning/gateway_url_pi/gateway_url_pi_865_mhz/test3.py", line 24, in <module>
    print Timer(self_add_tx()).timeit()
  File "C:\Python27\lib\timeit.py", line 129, in __init__
    compile(setup + '\n' + stmt, dummy_src_name, "exec")
  File "<timeit-src>", line 2
    0013A20040EA6DEC
                   ^
SyntaxError: invalid syntax

I am unable to umderstand the error. Does anyone know of this issue? My code works perfectly without this Timer.timeit().

JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
abhi1610
  • 721
  • 13
  • 28

1 Answers1

0

You need to pass the function to Timer() as a string. In your current syntax, it executes self_add_tx() and passes the result to Timer() for execution.

Try this syntax instead:

print Timer('self_add_tx()').timeit()
tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • Thanks for the answer. But It still not giving me the time required for execution. It shows the `Traceback (most recent call last): File "I:/HUB/Python_learning/gateway_url_pi/gateway_url_pi_865_mhz/test3.py", line 24, in print Timer('self_add_tx()').timeit() File "C:\Python27\lib\timeit.py", line 201, in timeit timing = self.inner(it, self.timer) File "", line 6, in inner NameError: global name 'self_add_tx' is not defined` – abhi1610 May 07 '16 at 06:43