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
Title-Author-data
Python Regular Expressions (RegEx) Basic Matching 12
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Timer and Time Measurement (time module) 9
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Iterators (Iterator Usage) 10
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Inheritance (OOP) 5
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Classes and Object Creation (OOP) 8
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Dictionary Sorting (By Key / By Value) 4
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Logging Implementation 4
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Exception Handling (try-except) 8
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python CSV File Reading and Processing 6
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python JSON Data Handling (Read/Write) 6
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python File Reading and Writing (Text Files) 3
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Decorators: Implementation and Usage 11
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Generators: Implementation and Usage 6
no_profile mrkorea쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Lambda Functions 9
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New
Python Remove Duplicate Elements from a List 11
no_profile goposu쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물
09-29 New

visit

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