-2
def Xbonacci(signature,n):
    count = 0
    while len(signature) != n:
        sum = 0
        for i in signature[count:]:
            sum = sum + i
        signature.append(sum)
        count += 1
    return signature


print(Xbonacci([1,0,0,0,0,0,0,0,0,0], 20))

My code excutes correctly and there isnt any errors, but it apparently takes longer than it should.

How can i find out the time taken to execute the program?

P.S This is a challenge on Codewars.

Please dont optimize my code, thats what i'd like to do once i get the execution time sorted.

  • 2
    Does this answer your question? [How can I time a code segment for testing performance with Pythons timeit?](https://stackoverflow.com/questions/2866380/how-can-i-time-a-code-segment-for-testing-performance-with-pythons-timeit) – Abhinav Mathur Mar 23 '21 at 16:56
  • This seems good answer for your questions: https://stackoverflow.com/questions/5622976/how-do-you-calculate-program-run-time-in-python – sainiajay Mar 23 '21 at 17:00

1 Answers1

0

time module is what you need

import time

def Xbonacci(signature,n):
    start = time.time()
    count = 0
    while len(signature) != n:
        sum = 0
        for i in signature[count:]:
            sum = sum + i
        signature.append(sum)
        count += 1
    end = time.time()
    print(f'Time elapsed: {end - start}')
    return signature


print(Xbonacci([1,0,0,0,0,0,0,0,0,0], 20))

if you are creating multiple functions consider making a time decorator