0

i defined a function,say

def func1(id):
    ....
    logic
    dataframe2.to_sql(query)
    ....

which runs some operations and store the result in database

and i have a table in pandas dataframe which has id column in it,and about 1000 rows with distinct id's

i will be calling this function using .apply function.

df['id'].apply(func1)

which will take one id at a time and do operations and store it in database , and same will happen for all id in my dataframe.

Is there any way to calculate how much total time my function took to run for all id's in df?

i was thinking maybe use time package and do smthing with it. but i am a beginner.

import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

is there any way to do this?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Shubham R
  • 7,382
  • 18
  • 53
  • 119

1 Answers1

0

you also can use context manager to check it. it could save some time when you want to reuse it.

>>> class TimeMyFunc(object):
...      def __enter__(self):
...          self.begin = time.time()
...      def __exit__(self,*args):
...          elapsed = time.time() - self.begin
...          print elapsed
...          
>>> with TimeMyFunc():
...     time.sleep(1)
...     
1.0
galaxyan
  • 5,944
  • 2
  • 19
  • 43