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.2.tar.gz (3.7 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.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: time_it_profiler-1.0.2.tar.gz
  • Upload date:
  • Size: 3.7 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.2.tar.gz
Algorithm Hash digest
SHA256 f1ebb59fa75418f618774ac30fe9bc59c535262745ed0fed89bc2805141445de
MD5 7252d0994837e38ea7856f4c95903ec5
BLAKE2b-256 e4270d586e604805c1fab73cd7ef2a8ae638ef0143970ffc58bfa5d91a495c0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for time_it_profiler-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 834d2ac315148f585cb92ecbf3664f43e5ed16038c1cdb217ce3e853ab95e581
MD5 c284ff1c37b1bb31d7091723f0f16715
BLAKE2b-256 4eff4d9a8c53d9e813aeea7d3c925248d26443297a369b96ca516215e2667f6d

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