How to find if any python code I'm using is the most efficient

Viewed 27

To be more clear, let's say I want to check if a character is in a string and I found 2 ways

 if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")

and other way is

if fullstring.find(substring) != -1:
    print("Character found")
  else:         
    print("Character not found")

now how to find which method is efficient i.e which executes faster? Myself being a rookie in python will do something like this This answer is not useful You can use time.time() or time.clock() before and after the block you want to time.

import time

t0 = time.time()
code_block
t1 = time.time()

total = t1-t0

is this the best way to test whether the code is the fastest/best performing?

0 Answers
Related