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

Introduction to the method of recording program running time in Python

零下一度
零下一度Original
2017-07-16 11:51:142371browse

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()

The output is as follows:

➜  Python python time_1.py
0:00:00.280797

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

Method 2


start = time.time()
run_fun()
end = time.time()
print end-start

Method 3

start = time.clock()
run_fun()
end = time.clock()
print end-start

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!

Statement:
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