在Python中使用time.time()的执行时间

时间:2016-03-17 10:36:33

标签: python python-2.7

我为HackerRank问题编写了一些代码(https://www.hackerrank.com/challenges/acm-icpc-team)。

import time
from itertools import combinations

start_time = time.time()

n,m = raw_input().strip().split(' ') # n = no of people and m = no of topics
n,m = [int(n),int(m)]
topic = []
topic_i = 0
for topic_i in xrange(n):
   topic_t = str(raw_input().strip())
   topic.append(topic_t)    # populate the topic[] list with the topics

counts = []
for list1, list2 in combinations(topic, 2):
   if list1 != list2:
      count = 0
      for i in xrange(m):
        if int(list1[i]) | int(list2[i]):
            count += 1
      counts.append(count)

print max(counts)
print counts.count(max(counts))

print time.time() - start_time

当我尝试运行代码时,执行时间为8.37576699257秒。但是我的节目很快就结束了。我已经读过timeit()函数默认运行传递给它的函数一百万次。类似的事情发生在这里吗?

1 个答案:

答案 0 :(得分:4)

您计算了程序等待用户输入的时间。您可能希望将第time.time()次调用移至raw_input()以下。