Skip to main content

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

Project description

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

📖 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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: time_it_profiler-1.0.0.tar.gz
  • Upload date:
  • Size: 3.4 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.0.tar.gz
Algorithm Hash digest
SHA256 bd4bd53c3e247821228d177443722dad0cc2aa679fc767202b8f97873ae010b9
MD5 00b98a175f01cf0c38713d4f5462e895
BLAKE2b-256 365e8a056b8e62c84f073c2d2acca769405d776b6f7958128c736ded56c30f84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for time_it_profiler-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48322fafc4ddaeba982d929157a6f955cb627797a0ecb60d9bfd0ba06015aacd
MD5 c4c39c08b2ccef4b9da6416415786a95
BLAKE2b-256 f517bf83b62483d3c1a575a302f2b344d92c782cd6028e7787f0c0433a318d43

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