简体   繁体   中英

Execution time using time.time() in Python

I wrote some code for a HackerRank problem ( 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

When I try to run the code, I get an execution time of 8.37576699257 seconds. But my program got over in a jiffy. I have read that the timeit() function, by default, runs the function passed to it a million times. Does something similar happen here?

You counted the time when the program waited for the user input too. You may want to move the first time.time() invocation below raw_input() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM