Skip to main content

A lightweight Python decorator for measuring execution time and system resource usage.

Project description

time_it - Python Function Profiler

🚀 Overview

time_it is a lightweight Python decorator that measures execution time, memory usage, and CPU time of any function. It is designed to be efficient and flexible, allowing you to enable or disable profiling dynamically.

📌 Features

Execution Time Measurement (High-precision timing with perf_counter)
Memory Usage Tracking (Peak memory usage in MB)
CPU Time Monitoring (User & System CPU time)
Zero Overhead When Disabled (measure_time=False bypasses all profiling logic)
Easy to Use as a Decorator (@time_it)
Lightweight & No External Dependencies


📥 Installation

If using this as a standalone module, simply clone the repository:

git clone https://github.com/brianvess/time_it.git
cd time_it

Or install it as a package (when published to PyPI):

pip install time-it-profiler

📖 Usage

Basic Example

from timeit_function import time_it

@time_it(measure_time=True)  # Enable profiling
def sample_function():
    total = sum(range(1, 1000000))
    return total

sample_function()

Example Output:

Execution Time: 0.012345 seconds
Memory Usage: 1.23 MB
User CPU Time: 0.002345 seconds
System CPU Time: 0.000678 seconds

Disable Profiling (Zero Overhead)

@time_it(measure_time=False)  # No profiling, function runs normally
def quick_function():
    return sum(range(1, 100))

quick_function()

🔍 How It Works

time_it Function

import time
import resource
from functools import wraps

def time_it(measure_time=True):
    def decorator(func):
        if not measure_time:
            return func  # Return the function unmodified if profiling is disabled

        @wraps(func)
        def wrapper(*args, **kwargs):
            start_time = time.perf_counter()
            start_resources = resource.getrusage(resource.RUSAGE_SELF)
            
            result = func(*args, **kwargs)
            
            end_time = time.perf_counter()
            end_resources = resource.getrusage(resource.RUSAGE_SELF)

            memory = (end_resources.ru_maxrss - start_resources.ru_maxrss) / 1024  # Convert KB to MB
            user_time = end_resources.ru_utime - start_resources.ru_utime
            system_time = end_resources.ru_stime - start_resources.ru_stime

            print(
                f"Execution Time: {end_time - start_time:.6f} seconds\\n"
                f"Memory Usage: {memory:.2f} MB\\n"
                f"User CPU Time: {user_time:.6f} seconds\\n"
                f"System CPU Time: {system_time:.6f} seconds"
            )

            return result
        return wrapper
    return decorator

🛠️ Contributing

  1. Fork this repository.
  2. Create a new branch:
    git checkout -b feature-branch
    
  3. Commit your changes:
    git commit -m "Added new feature"
    
  4. Push the changes:
    git push origin feature-branch
    
  5. Submit a pull request! 🎉

📜 License

This project is licensed under the MIT License. See LICENSE for details.


🌟 Credits

Developed by Brian Vess.
If you find this useful, please ⭐ star this repository and contribute! 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

time_it_profiler-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

time_it_profiler-0.0.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file time_it_profiler-0.0.2.tar.gz.

File metadata

  • Download URL: time_it_profiler-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for time_it_profiler-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1a6eaf774ec3e30e0aad3b6609064b3746f1bd5b68faf2e148857b8f15356896
MD5 062c8db532a67365ccdf0a4a9f3e8462
BLAKE2b-256 ab6a4d2206face63d58dc9a7307a5f9206940fd5d6e69c8cfe7dc07c6115e403

See more details on using hashes here.

File details

Details for the file time_it_profiler-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for time_it_profiler-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a1a8f53a6c549a39242e9b27529388a18d8a3a9f6097b9dae3c157db1dcb6a97
MD5 7d5e340f54f27a7506338fe3f88ee7b4
BLAKE2b-256 7d923be5f15771d8131e6fcbb483e33711d09af925cb23d96ab9ad295c9f17a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page