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. Show
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:
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úcTí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
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.630ms0 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.630ms1 = 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.630ms3 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.630ms4 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.630ms5 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.630ms6 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.630ms7 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.630ms8 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.630ms9 The time of execution of above program is : 0.766ms0 The time of execution of above program is : 0.766ms1 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.630ms1 The time of execution of above program is : 0.766ms3 = The time of execution of above program is : 0.766ms5 The time of execution of above program is : 0.766ms6 The time of execution of above program is : 0.766ms6 The time of execution of above program is : 0.766ms8__ The time of execution of above program is : 71.161ms0____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.630ms0 The time of execution of above program is : 71.161ms3 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.630ms8 The time of execution of above program is : 71.161ms5 The time of execution of above program is : 71.161ms6 The time of execution of above program is : 71.161ms7 The time of execution of above program is : 71.161ms8 The time of execution of above program is : 71.161ms9 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ặpKiể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
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.630ms0 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.630ms1 = 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.630ms3 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.630ms4 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.630ms5 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.630ms6 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.630ms7 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.630ms8 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.630ms9 The time of execution of above program is : 0.766ms0 The time of execution of above program is : 0.766ms1 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.630ms1 The time of execution of above program is : 0.766ms3 = The time of execution of above program is : 0.766ms5 The time of execution of above program is : 0.766ms6 The time of execution of above program is : 0.766ms6 The time of execution of above program is : 0.766ms8__ The time of execution of above program is : 71.161ms0____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.630ms0 The time of execution of above program is : 71.161ms7 The time of execution of above program is : 71.161ms8 The time of execution of above program is : 71.161ms9 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.766ms1 The time of execution of above program is : 71.161ms3 = 1= 2The time of execution of above program is : 0.766ms9 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.630ms4 500 loops, best of 5: 503 usec per loop2 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.630ms6 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.630ms7 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.630ms8 The time of execution of above program is : 0.766ms8 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 loop8 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.766ms8__ Python3The time of execution of above program is : 0.766ms1 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.630ms0 The time of execution of above program is : 0.766ms1 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.630ms1 = 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.630ms3 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.630ms1 = 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.630ms3 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.630ms4 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.630ms5 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.630ms6 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.630ms7 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.630ms8 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.630ms9 The time of execution of above program is : 0.766ms0 The time of execution of above program is : 0.766ms1 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.630ms1 The time of execution of above program is : 0.766ms3 = The time of execution of above program is : 0.766ms5 The time of execution of above program is : 0.766ms6 The time of execution of above program is : 0.766ms6 The time of execution of above program is : 0.766ms8__ The time of execution of above program is : 71.161ms0____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.630ms0 The time of execution of above program is : 71.161ms7 The time of execution of above program is : 71.161ms8 The time of execution of above program is : 71.161ms9 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.161ms3 = 1Iteration: 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.630ms35 The time of execution of above program is : 0.766ms9 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 timeKiể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:
Ví dụ 1: Sử dụng thời gian bên trong đoạn mã Python để đo thời gian thực hiệnPython3
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.630ms38 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.630ms39__ 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.630ms41 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.630ms42 = 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.630ms44 = 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.630ms46 = 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.630ms48 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.630ms49 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.630ms50 = 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.630ms52 Các The time of execution of above program is : 71.161ms3 = 1Iteration: 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.630ms65 The time of execution of above program is : 0.766ms9 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ệnChú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:
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 |