Home > Backend Development > Python Tutorial > Introduction to the method of recording program running time in Python

Introduction to the method of recording program running time in Python

零下一度
Release: 2017-07-16 11:51:14
Original
2400 people have browsed it

This article mainly introduces python related information on several methods of recording program running time. Friends who need it can refer to

The first one I saw handwritten, similar to the following Type:

import datetime
  def time_1():
    begin = datetime.datetime.now()
     sum = 0
   for i in xrange(10000000):
     sum = sum + i
   end = datetime.datetime.now()
    return end-begin
 print time_1()
Copy after login

The output is as follows:

➜  Python python time_1.py
0:00:00.280797
Copy after login

Three methods of python recording program running time

Python is provided here Three methods of recording program running time, with implementation code and final comparison. For your reference:

Method 1

import datetime
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
Copy after login

Method 2


start = time.time()
run_fun()
end = time.time()
print end-start
Copy after login

Method 3

start = time.clock()
run_fun()
end = time.clock()
print end-start
Copy after login

Both methods 1 and 2 include the time that other programs use the CPU, which is the program The running time from start to end of program.

Method 3 only calculates the CPU time of program running

The above is the detailed content of Introduction to the method of recording program running time in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template