当前位置: 动力学知识库 > 问答 > 编程问答 >

Cannot get value from second call of time.clock() in Python

问题描述:

I am trying to calculate the time taken between two function calls.

import time

class timer():

def __init__(self)

self.start_time = 0

self.stop_time = 0

def start_stream(self):

self.start_time = time.clock()

def stop_stream(self):

self.stop_time = time.clock()

print ("Time elapsed: %s % self.stop_time - self.start_time)

start_stream()

stop_stream()

I am always allowed to read/print time.clock() the first time, but when trying to save the stop time I get

AttributeError: 'int' object has no attribute 'clock'

What am I doing wrong?

网友答案:

Somewhere else in the code you have assigned an integer to global name time. This happens after calling start_stream(), but before calling stop_stream().

The answer is quite simple: use a different global name for that integer value, or try to avoid using a global for that value.

网友答案:

I think couple of mistake are there here, i have modified the code a bit

import time

class Stop_watch():

    def __init__(self):
        self.start_time = 0
        self.stop_time = 0

    def start_stream(self):
        self.start_time = time.clock()

    def stop_stream(self):
            self.stop_time = time.clock()
            print ("Time elapsed: %s"% (self.stop_time - self.start_time))

call this module in your function and use it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: