sending an email through gmail using python

How To Calculate Time Taken To Read A CSV File in Python?

In Software Application development time taken by a computer program in retrieving data from a file should be as optimum as possible. CSV(Comma Separated Value) file format is one of the widely used formats to store data records.

In this blog, I will share the programming code written in Python which will illustrate following things:

  • Read a CSV File.
  • Retrieve its values in the console.
  • Calculate total number of rows in a CSV File.
  • Calculate time taken to read all those entries in a CSV File.
import csv, time
def csvReader():
    with open('businessfile.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
    print('There are %d rows in the CSV file.'%rowCount)
initialTime = time.time()
finalTime= time.time()
print('The CSV Reader took %s seconds to read the file.' %(finalTime - initialTime))

Two python modules csv and time are used in above program code.  Let’s have a general walk through of the code below.

  1. csv and time modules/libraries are imported with the import statement. L1
  2. A csvReader function is defined. L2
  3. Initially the row count of csv file is set to 0. L3
  4. CSV File businessfile.csv is opened and csv.reader reads the csv file. L4-L5
  5. A for loop prints each row of the CSV File in your console and rowCount is incremented by 1. L6-L8
  6. time.time() records your current system time. two variables initialTime are finalTime are defiend with the calue of current timestamp before and after the csvReader executing funciton respectivels. L10-L12
  7. A time difference is calculated in the last line L14.

I created a dummy csv file whose file size is 15MB. CSV file has following headers and similar data are added:

Business plans,manufacture,0,619 employees,D0201_01,18822

You can find the complete code and the CSV file here in my GitHub repository:

Running the code python3 I get following output:

There are 111111 rows in the CSV file.
The CSV Reader took 6.583252906799316 seconds to read the file.