0

I am trying to see how I can check the time taken by python to write the data inside a csv file. Here is a sample code that I am trying to work:

import timeit
start_time = timeit.default_timer()
df_text.to_csv('D:\\DF.csv', index = False)
print(timeit.default_timer() - start_time)

More specifically, How can I see the write time taken by df_text to fill in the data inside DF.csv? I believe this code (in 3rd) tells me the time taken to create the CSV file inside the directory.

  • 2
    Does this answer your question? [How to use timeit module](https://stackoverflow.com/questions/8220801/how-to-use-timeit-module) – sushanth Apr 16 '21 at 04:26
  • The code you have does this. Does it not work for you? – Tim Roberts Apr 16 '21 at 04:38
  • Hi Sushanth, thanks for the suggestion, if i use timeit in my existing code it tells me how much time it takes by python to create this csv file. I need to see the time taken by df_text to fill the csv file. – Alex MAthew Apr 16 '21 at 04:38
  • Also note that you need EITHER the `r` prefix or the double slashes, not both: `r'D:\DF.csv'` or `'D:\\DF.csv'` – Tim Roberts Apr 16 '21 at 04:39
  • Hi Robert, this code only tells me the time taken to create the csv file in my directory. I want to see the time taken by df_text to fill all the values inside the csv file. – Alex MAthew Apr 16 '21 at 04:39
  • 2
    The time there includes both operations. It takes no time at all to open a file, so you can assume that the bulk of your time is writing the values. – Tim Roberts Apr 16 '21 at 04:40
  • @AlexMAthew as said below, the time to open a file is probably insignificant enough that it won't even show in your result, especially if you are using seconds. And if the file is already created, it should just open it – Brandon Kauffman Apr 16 '21 at 09:48
  • @TimRoberts I added a snippet code, request you to please let me know if I am doing it right? – Alex MAthew Apr 17 '21 at 00:18
  • That's not going to tell you anything new. You already know how long `df_text.to_csv` takes. You did that in your first snippet. `to_csv` creates AND FILLS the CSV file, and I"m not sure why you think there's more to it. – Tim Roberts Apr 17 '21 at 03:38
  • Ya, I got confused, thank you! – Alex MAthew Apr 17 '21 at 04:22

1 Answers1

0

Your way is correct, another way to check average time taken by a function is to run it multiple times. Can be done using Timeit Module