Course Content

Time Module In Python

Welcome to another tutorial guys. We are just going to open our Pycharm ide and start this tutorial without wasting any time.

 

 

We are going to name our file time123 here. Moving towards our today's topic.

In Tutorial #38, we discussed modules in detail, along with their types. We also discussed a few important modules in that tutorial. If you have not gone through it, then I would recommend you to go and see that one first. The execution time of a program is defined as the system's time to execute the task. As we know, all program takes some execution time, but we don't know how much. So, don't worry. In this tutorial, we will learn it by using a very helpful module known as the "Time module." 

               As can be defined by the name, "time module handles time-related tasks."

It can be accessed by simply using an import statement.

import time

 

 In Python, time can be tracked through its built-in libraries. The time module consists of all time-related functions. It also allows us to access several types of clocks required for various purposes. We will be discussing some of these important functions that are commonly used and come handy for further programming.

time.time():

               It returns us the seconds of time that have elapsed since the Unix epoch. In simple words, it tells us the time in seconds that have passed since 1 January 1970. Its syntax is simple and easy to use.

import time
seconds = time.time()
print("Seconds since epoch =", seconds)
time.asctime():

       We use the function time.asctime() to print the local time onto the screen. There are a lot of other ways to do it but time.asctime() prints the time in a sequence using a 24 characters string. 

The format will be something like this: Mon Feb 10 08:01:02 2020

Time.sleep():

What sleep() function does is, it delays the execution of further commands for given specific seconds. In simple terms, it sends the program to sleep for some defined number of seconds. sleep() function is mostly used in programs directly connected to the operating system and in-game development. It halts the program execution, giving other programs a chance to get executed simultaneously. 

The syntax is :

time.sleep(5)

The number of seconds is sent as a parameter within parenthesis. The program will go to sleep for 5 seconds after getting to this line of code and will continue its execution afterward.

 time localtime():

The time.localtime() is used to convert the number of seconds to local time. This function takes seconds as a parameter and returns the date and time in time.struct_time format. It is optional to pass seconds as a parameter. If seconds is not provided, the current time will be returned by time() is used.

The syntax is:

time.localtime([ sec ])

For Example:

import time

print "time.localtime() returns: %s",%time.localtime()

Uses of time modules:

We can use the time module 

  • In games where missions depend on a certain time limit.
  • To check the execution time a certain part of our code is taking.
  • To print the date or local time onto the screen
  • To suspend the execution of python thread.
  • To measure the efficiency of the code.

There are many built-in functions in the time module. Not all of them are discussed in this tutorial. Explore more time module functions and use them in your code, so that you can measure the execution time of your code. 

Code file as described in the video

import time
initial = time.time()

k = 0
while(k<45):
    print("This is harry bhai")
    time.sleep(2)
    k+=1
print("While loop ran in", time.time() - initial, "Seconds")

initial2 =time.time()
for i in range(45):
    print("This is harry bhai")
print("For loop ran in", time.time() - initial2, "Seconds")


# localtime = time.asctime(time.localtime(time.time()))
# print(localtime)
  

Be the first person to comment!

Comments(0)

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here