Skip to main content

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

Project description

Save the updated README.md file for PyPI as a downloadable markdown file

time-it-profiler - Python Function Profiler

🚀 Overview

time-it-profiler is a lightweight Python decorator that measures execution time, memory usage, and CPU time of any function. It is designed to be efficient, flexible, and easy to integrate into any Python project.

📌 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

Install directly from PyPI using:

pip install time-it-profiler

📂 GitHub Repository


📖 Usage

Basic Example

from time_it 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

Contributions are welcome! Fork the repository on GitHub and submit a pull request.

🔗 GitHub Repo: https://github.com/brianvess/time_it.git

  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-1.0.1.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-1.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: time_it_profiler-1.0.1.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-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c70c46070369fbc3b6e78c425f859fd49d47f92c6e152ac83c582cba3a0bce4d
MD5 43ff7d0f4ccaeeb3e6ef2d6ca0e8c6a5
BLAKE2b-256 ecfb35889585f1831b9aa8637bfe9b768880576022dd29677e8d90f40fd75a00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for time_it_profiler-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d65b7321213bd7f0a231243be84949d10c126d53d7e17deb84507062cc078b96
MD5 f476c0b410925ad699e24abeeaf54b86
BLAKE2b-256 83df4415c66b8cd37be91f26390a5fd70a1f7c61e526dec8b62b248b5735237c

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