Skip to content
Advertisement

Python 3: time.perf_counter() output does not match the programme processing time on Coursera

I have been working on a Coursera assignment, it required my run time to be less than 5.00 . The code is below, but my question is basically about the first and last line, where I was testing the processing time

import time

def pairwise_product(number):
  n = len(number)
  product = 0   
  for i in range(n):
    for j in range(i+1, n):
        product = max(product, number[i] * number[j])       
  return product

input_length = int(input())
input_number = [int(x) for x in input().split()]
print(pairwise_product(input_number))

print(time.perf_counter())

Using the last line of code, my processing time was 3.3251947

However, when I submitted this code onto Coursera, I have failed my assignment because according to the system, my processing time was 9.99

enter image description here

I am very confused, because since the runtime on my computer is inconsistent with the runtime on Coursera, it means I am no longer able to debug and test my program before submitting it. Is it to do with my programme?

Thank you a lot!

Advertisement

Answer

Different computers run at different speeds. It’s not surprising that Coursera’s grader is slower than your machine, since autograders/online judges are often run on old machines. Old slow machines are cheap and the grading service is not a revenue source.

About the best you can do is try to estimate the ratio by which your machine is faster. But beware: Coursera’s grader stops at 10 seconds for a five-second limit. So you don’t actually know that the grader completed running your program; it might be a lot more than three times as slow.

In most cases the time limit for a task will be much higher than necessary, to avoid this sort of problem. If you get a TLE, you probably need to find a better algorithm

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement