Python Timer and Time Measurement (time module) > Python

Entire search within the site

Python

Python Timer and Time Measurement (time module)

Page Info

Content

Python Timer and Time Measurement (time module)

In Python, the time module allows you to measure execution time, create delays, and work with timestamps. It is commonly used for performance testing and benchmarking code.

Measuring Execution Time


import time

# Record start time
start_time = time.time()

# Code block to measure
total = 0
for i in range(1, 1000000):
    total += i

# Record end time
end_time = time.time()

# Calculate elapsed time
print(f"Elapsed time: {end_time - start_time:.4f} seconds")
  

Explanation:

  • time.time(): Returns the current time in seconds since the epoch (floating point).
  • Subtract start time from end time to get elapsed time.

Using time.sleep() to Pause Execution


import time

print("Start")
time.sleep(3)  # Pause execution for 3 seconds
print("End after 3 seconds")
  

Explanation:

  • time.sleep(seconds): Delays program execution for the specified number of seconds.
  • Useful for creating pauses or simulating delays in programs.

Using time.perf_counter() for High-Precision Timing


import time

start = time.perf_counter()

# Code block to measure
sum_value = sum(range(1, 1000000))

end = time.perf_counter()

print(f"High-precision elapsed time: {end - start:.6f} seconds")
  

Explanation:

  • time.perf_counter() provides a high-resolution timer for more precise measurement.
  • Ideal for benchmarking small code blocks or performance-critical operations.

Key Points

  • time.time(): Measures wall-clock time in seconds.
  • time.sleep(): Pauses execution for a given time.
  • time.perf_counter(): High-resolution timer for accurate measurements.
  • Useful for profiling code, performance testing, and adding delays in Python programs.

SEO Keywords

Python timer example, Python measure execution time, Python time module, Python time.sleep, Python perf_counter, Python benchmarking code

Using Python's time module, you can efficiently measure the execution duration of code blocks, pause program execution, and perform high-precision benchmarking.

Good0 Bad0

댓글목록

등록된 댓글이 없습니다.

Python

Latest Posts

Total 31 posts | Page 1
Python List
No Title Author Views Likes Dislikes Date
31 Python Regular Expressions (RegEx) Basic Matching New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 11 0 0 09-29
30 Python Timer and Time Measurement (time module) New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7 0 0 09-29
29 Python Iterators (Iterator Usage) New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5 0 0 09-29
28 Python Inheritance (OOP) New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4 0 0 09-29
27 Python Classes and Object Creation (OOP) New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7 0 0 09-29
26 Python Dictionary Sorting (By Key / By Value) New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3 0 0 09-29
25 Python Logging Implementation New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3 0 0 09-29
24 Python Exception Handling (try-except) New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4 0 0 09-29
23 Python CSV File Reading and Processing New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4 0 0 09-29
22 Python JSON Data Handling (Read/Write) New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5 0 0 09-29
21 Python File Reading and Writing (Text Files) New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3 0 0 09-29
20 Python Decorators: Implementation and Usage New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8 0 0 09-29
19 Python Generators: Implementation and Usage New no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5 0 0 09-29
18 Python Lambda Functions New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8 0 0 09-29
17 Python Remove Duplicate Elements from a List New no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10 0 0 09-29

visit

today
50
yesday
144
maxday
144
allday
194
Copyright © https://goposu.com/g All rights reserved.