Hướng dẫn python count time execution - thời gian đếm trăn thực thi

Trong bài viết này, chúng tôi sẽ thảo luận về cách kiểm tra thời gian thực hiện của tập lệnh Python.

Có nhiều mô -đun Python như mô -đun Thời gian, Thời gian và DateTime trong Pythonwhich có thể lưu trữ thời gian mà một phần cụ thể của chương trình đang được thực thi. Bằng cách thao túng hoặc nhận được sự khác biệt giữa thời gian bắt đầu và kết thúc mà tại đó một phần cụ thể đang được thực thi, chúng ta có thể tính toán thời gian để thực hiện phần. & NBSP;

Các phương pháp sau đây có thể được sử dụng để tính toán thời gian khác biệt:

  • Mô-đun thời gian Python cung cấp các chức năng liên quan đến thời gian khác nhau. Mô -đun này thuộc các mô -đun tiện ích tiêu chuẩn Python. Thời gian.time () Phương thức của mô -đun thời gian được sử dụng để có thời gian tính bằng giây kể từ kỷ nguyên. Việc xử lý Leap Seconds phụ thuộc vào nền tảng.time.time() method of the Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform-dependent.
  • Mô -đun DateTime Python xác định một hàm có thể được sử dụng chủ yếu để có được thời gian và ngày hiện tại. Bây giờ () hàm trả về ngày và giờ địa phương hiện tại, được xác định trong mô -đun DateTime. datetime module defines a function that can be primarily used to get the current time and date. now() function Return the current local date and time, which is defined under the datetime module.
  • Mô -đun Pythontimeit chạy đoạn mã N Số lần của bạn (giá trị mặc định là, 1000000) để bạn có được phép đo thời gian thực hiện mã có liên quan nhất về mặt thống kê. timeit module runs your snippet of code n number of times (the default value is, 1000000) so that you get the statistically most relevant measurement of code execution time.

Sử dụng mô -đun thời gian Kiểm tra thời gian thực hiện của Python & NBSP;

Ví dụ 1: Đo thời gian thực hiện cho một phân đoạn mã bằng cách ghi lại thời gian bắt đầu và kết thúc

Tính toán thời gian bằng hàm mô -đun thời gian và thời gian.time (). Chúng tôi đã tính toán thời gian của chương trình trên, xuất phát từ Lệnh 10^-3. Chúng tôi có thể kiểm tra thời gian bằng cách tăng số lượng tính toán bằng cách sử dụng cùng một thuật toán.

Python3

import time

start =

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
0

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
3

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
4
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
5
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
6
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
7
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
8
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
9
The time of execution of above program is : 0.766ms
0

The time of execution of above program is : 0.766ms
1
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1
The time of execution of above program is : 0.766ms
3=
The time of execution of above program is : 0.766ms
5
The time of execution of above program is : 0.766ms
6
The time of execution of above program is : 0.766ms
6
The time of execution of above program is : 0.766ms
8__

The time of execution of above program is : 71.161ms
0____9
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
0

The time of execution of above program is : 71.161ms
3
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
8
The time of execution of above program is : 71.161ms
5
The time of execution of above program is : 71.161ms
6

The time of execution of above program is : 71.161ms
7
The time of execution of above program is : 71.161ms
8
The time of execution of above program is : 71.161ms
9
python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
0__26

Output:

The time of execution of above program is : 0.77056884765625 ms

Ví dụ 2: Đo thời gian thực hiện cho một phân đoạn mã bằng cách thêm thời gian cần thiết cho mỗi lần lặp

Kiểm tra thời gian thực hiện chương trình cho các số lượng tính toán khác nhau. Chúng tôi thấy một xu hướng chung trong việc tăng thời gian tính toán để tăng số lượng thực thi. Tuy nhiên, nó có thể không hiển thị bất kỳ xu hướng tuyến tính hoặc gia số cố định.

Python3

import time

start =

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
0

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
3

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
4
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
5
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
6
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
7
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
8
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
9
The time of execution of above program is : 0.766ms
0

The time of execution of above program is : 0.766ms
1
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1
The time of execution of above program is : 0.766ms
3=
The time of execution of above program is : 0.766ms
5
The time of execution of above program is : 0.766ms
6
The time of execution of above program is : 0.766ms
6
The time of execution of above program is : 0.766ms
8__

The time of execution of above program is : 71.161ms
0____9
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
0

The time of execution of above program is : 71.161ms
7
The time of execution of above program is : 71.161ms
8
The time of execution of above program is : 71.161ms
9
python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
0__26

The time of execution of above program is : 0.766ms
1
The time of execution of above program is : 71.161ms
3=1=2
The time of execution of above program is : 0.766ms
9

Output:

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms

Ví dụ 2: Đo thời gian thực hiện cho một phân đoạn mã bằng cách thêm thời gian cần thiết cho mỗi lần lặp Here we have truncated the output for representation purpose. But if we compare the iterations from 100 to 700 they are less than 1ms. But towards the end of the loop, each iteration taking ~7ms. Thus, there is an increase in time taken as the number of iterations have increased. This is generally because, the inner loop iterate more number of time depending on each outer iteration.

Kiểm tra thời gian thực hiện chương trình cho các số lượng tính toán khác nhau. Chúng tôi thấy một xu hướng chung trong việc tăng thời gian tính toán để tăng số lượng thực thi. Tuy nhiên, nó có thể không hiển thị bất kỳ xu hướng tuyến tính hoặc gia số cố định.

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
4
500 loops, best of 5: 503 usec per loop
2
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
6
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
7
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
8
The time of execution of above program is : 0.766ms
8
python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
6
500 loops, best of 5: 503 usec per loop
8
python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
6
The time of execution of above program is : 0.766ms
8__

Python3

The time of execution of above program is : 0.766ms
1start __
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
0

The time of execution of above program is : 0.766ms
1
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
3

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
3

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
4
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
5
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
6
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
7
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
8
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
9
The time of execution of above program is : 0.766ms
0

The time of execution of above program is : 0.766ms
1
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
1
The time of execution of above program is : 0.766ms
3=
The time of execution of above program is : 0.766ms
5
The time of execution of above program is : 0.766ms
6
The time of execution of above program is : 0.766ms
6
The time of execution of above program is : 0.766ms
8__

The time of execution of above program is : 71.161ms
0____9
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
0

The time of execution of above program is : 71.161ms
7
The time of execution of above program is : 71.161ms
8
The time of execution of above program is : 71.161ms
9
python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
0__26

The time of execution of above program is : 71.161ms
3=1
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
35
The time of execution of above program is : 0.766ms
9

Output:

The time of execution of above program is : 0.766ms

Ví dụ 2: Đo thời gian thực hiện cho một phân đoạn mã bằng cách thêm thời gian cần thiết cho mỗi lần lặp timeit module check the execution time

Kiểm tra thời gian thực hiện chương trình cho các số lượng tính toán khác nhau. Chúng tôi thấy một xu hướng chung trong việc tăng thời gian tính toán để tăng số lượng thực thi. Tuy nhiên, nó có thể không hiển thị bất kỳ xu hướng tuyến tính hoặc gia số cố định.timeit() method to do the same. The module function timeit.timeit(stmt, setup, timer, number) accepts four arguments:

  • Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    4
    500 loops, best of 5: 503 usec per loop
    2
    Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    6
    Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    7
    Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    8
    The time of execution of above program is : 0.766ms
    8
    python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
    6
    500 loops, best of 5: 503 usec per loop
    8
    python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"
    6
    The time of execution of above program is : 0.766ms
    8__
  • The time of execution of above program is : 0.766ms
    1start __
    Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    0
  • The time of execution of above program is : 0.766ms
    1
    Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    1=
    Iteration: 100    Time taken: 0.105ms
    Iteration: 200    Time taken: 0.191ms
    Iteration: 300    Time taken: 0.291ms
    Iteration: 400    Time taken: 0.398ms
    Iteration: 500    Time taken: 0.504ms
    Iteration: 600    Time taken: 0.613ms
    Iteration: 700    Time taken: 0.791ms
    ...
    Iteration: 5400    Time taken: 6.504ms
    Iteration: 5500    Time taken: 6.630ms
    3
  • Số, là số lượng thực thi mà bạn muốn chạy STMT.

Ví dụ 1: Sử dụng thời gian bên trong đoạn mã Python để đo thời gian thực hiện

Python3

import

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
38

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
39__
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
41

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
42=

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
44=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
46=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
48

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
49
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
50=
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
52

Các

The time of execution of above program is : 71.161ms
3=1
Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms
65
The time of execution of above program is : 0.766ms
9

Output:

The time of execution of above program is : 71.161ms

Ví dụ 2: Sử dụng thời gian từ dòng lệnh để đo thời gian thực hiện

Chúng ta có thể đo thời gian được thực hiện bằng các câu lệnh mã đơn giản mà không cần phải viết các tệp Python mới, sử dụng giao diện Timeit CLI.

Timeit hỗ trợ các đầu vào dòng lệnh khác nhau, ở đây chúng tôi sẽ lưu ý một vài đối số phổ biến của MOS:

  • -s [TiếtSetUpup]: Mã thiết lập để chạy trước khi chạy câu lệnh mã.: Setup code to run before running the code statement.
  • -n [ChuyệnNumber]: Số lần để thực hiện câu lệnh.]: Number of times to execute the statement.
  • Tiếtp [CấmProcess]: Đo thời gian xử lý của quá trình thực thi mã, thay vì thời gian đồng hồ treo tường.p [–process]: Measure the process time of the code execution, instead of the wall-clock time.
  • Tuyên bố: Các câu lệnh mã để kiểm tra thời gian thực hiện, được lấy làm đối số vị trí.: The code statements to test the execution time, taken as a positional argument.

Tuyên bố CLI thời gian:

python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"

Output:

500 loops, best of 5: 503 usec per loop