1

Lets say I have a input with 10 million lines. I want to know how long it takes to do this:

if line not in list:
  list.append(line)

Is there any website or source of another kind that can tell me the approximate speed of various tasks?

AWE
  • 4,045
  • 9
  • 33
  • 42

2 Answers2

3

You can get the total running time of your program with this:

time python3 program.py

in your terminal. It will have output similar to:

real    0m0.184s
user    0m0.032s
sys     0m0.015s

You can get how long it takes a particular function to run n times with:

from timeit import timeit

def foo():
    return 123456789 + 987654321

n = 10000000
time = timeit(foo, number=n)
print(time)

You can also time how long a section of code takes with time:

from time import time

start = time()

# code to be timed

finish = time()
print(finish - start)
Scorpion_God
  • 1,499
  • 10
  • 15
1

run this command on terminal :

time python your_file.py

result must be like this :

real    0m0.018s
user    0m0.009s
sys     0m0.009s

that

real - refers to the actual elasped time 
user - refers to the amount of cpu time spent outside of kernel
sys - refers to the amount of cpu time spent inside kernel specific functions

read more about real,user,sys in THIS stachoverflow answer by ConcernedOfTunbridgeWells.

For find performance of ech line you must use a Line-by-line timing and execution frequency with a profiler, so line_profiler is an easy and unobtrusive way to profile your code and use to see how fast and how often each line of code is running in your scripts. you can install line_profiler that written by Robert Kern , you can install the python package via pip :

$ pip install line_profiler

read documentation HERE .

Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188