Skip to main content

MiniLoop is a lightweight Python game loop library designed to provide a simple and efficient way to manage update and render cycles in real-time applications.

Project description

MiniLoop

MiniLoop is a lightweight Python game loop library designed to provide a simple and efficient way to manage update and render cycles in real-time applications.


Why MiniLoop?

When developing real-time applications like games and simulations, having an efficient and stable game loop is crucial. MiniLoop is designed to provide a precise, high-performance loop with built-in timing management, making it a superior alternative to a basic while loop.

  • More Accurate Frame Timing

    • MiniLoop ensures consistent frame pacing by using a high-resolution timer. Unlike a simple while loop that can suffer from frame drift and timing inconsistencies, MiniLoop dynamically adjusts to keep the frame rate steady.
  • Improved CPU Efficiency

    • A basic while loop can waste CPU cycles when waiting for the next frame, leading to higher power consumption and unnecessary CPU usage. MiniLoop intelligently sleeps between frames, reducing resource usage while maintaining target FPS.
  • Higher FPS Stability From our benchmarks:

MiniLoop achieved 60.69 FPS While loop only reached 58.89 FPS This means MiniLoop maintains a smoother experience, preventing stuttering and inconsistencies.

  • Optimized Python Function Calls
    • MiniLoop caches function pointers before the loop starts, minimizing overhead when calling update() and render(). A plain while loop calls Python functions inefficiently, slowing down performance.
  • Cross-Platform & Easy to Use
    • Works on Windows, Linux, and macOS with precise timing control. Simple API: just set your update() and render() functions, and MiniLoop takes care of the rest.
  • Built-In Stop Handling
    • MiniLoop includes a clean stop mechanism that allows safe termination from Python, unlike a while loop that may require manual handling with try/except.

TL;DR

  • More stable FPS
  • Lower CPU usage
  • Better timing accuracy
  • Easier to use

If you're working on a game, simulation, or any real-time application, MiniLoop is the better choice. Try it out and experience a smoother, more efficient loop!

Features

  • High-performance game loop with adjustable FPS
  • Support for user-defined update and render callbacks
  • Cross-platform high-resolution timer
  • Simple start/stop functionality

Requirements

  • Python 3.x (recommended: 3.7 or higher)

Installation

Install the package using pip:

pip install miniloop

Usage

Here is an example of how to use MiniLoop:

import miniloop

def update(delta_time):
    print(f"Update: {delta_time:.5f} sec")

def render():
    print("Rendering frame")

miniloop.set_update(update)
miniloop.set_render(render)
miniloop.set_fps(60)  # Target 60 FPS

miniloop.start()

Output Example

Update: 0.01667 sec
Rendering frame
Update: 0.01665 sec
Rendering frame
...

Performance Benchmark: Miniloop vs While Loop

This benchmark compares the performance of Miniloop and a standard While Loop at different frame rates (FPS) and durations. The test evaluates both accuracy (how closely the achieved FPS matches the target) and stability (frame-time consistency).

Test Configuration

  • Durations Tested: 3s, 5s, 10s
  • FPS Targets: 30, 60, 120
  • Metrics Evaluated:
    • Achieved FPS: The actual FPS measured during execution.
    • Score (0-200): A combination of accuracy and stability.

Results

3-Second Tests

FPS Target Miniloop Achieved FPS Miniloop Score While Loop Achieved FPS While Loop Score
30 FPS 30.15 199.54 29.73 199.20
60 FPS 60.95 197.16 58.81 196.42
120 FPS 117.45 192.34 116.27 188.80

5-Second Tests

FPS Target Miniloop Achieved FPS Miniloop Score While Loop Achieved FPS While Loop Score
30 FPS 30.00 199.99 29.72 199.17
60 FPS 60.52 198.43 58.80 196.39
120 FPS 117.08 191.25 116.90 190.71

10-Second Tests

FPS Target Miniloop Achieved FPS Miniloop Score While Loop Achieved FPS While Loop Score
30 FPS 29.99 199.97 29.75 199.24
60 FPS 60.19 199.42 58.80 196.40
120 FPS 116.74 190.22 116.73 190.18

Final Performance Summary

  • Miniloop Average Score: 196.48 / 200
  • While Loop Average Score: 195.17 / 200

Conclusion

Miniloop outperformed the While Loop overall, achieving a slightly higher accuracy and stability in frame rates, particularly at higher FPS targets. While both methods performed well, Miniloop provided better consistency, especially for longer durations.

How It Works

MiniLoop miniloop runs a continuous loop, calling the user-defined update and render functions at a target frame rate. The update function receives delta_time, which represents the time elapsed since the last frame, allowing smooth time-based updates.

Limitations

  • Currently limited to single-threaded execution.
  • No built-in event handling; input handling must be managed externally.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.

Local Development

To set up a local development environment, follow these steps:

  1. Create and activate a virtual environment:
    source venv/bin/activate  # macOS/Linux
    venv\Scripts\activate     # Windows
    venv/Scripts/activate
    
  2. Build & install the project
    python setup.py build
    python setup.py install
    

The module is now installed in the virtual environment. You can test it by running the example script:

python examples/example.py

License

This project is licensed under the Apache-2.0 License. See the LICENSE file for details.

Author

Developed by Sekiraw

Acknowledgements

Thanks to the Python development community for providing tools and resources to make this project possible.

This styling ensures clarity, proper sectioning, and good readability. Let me know if you`d like any further adjustments!

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

miniloop-1.0.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

miniloop-1.0.1-cp313-cp313-win_amd64.whl (14.3 kB view details)

Uploaded CPython 3.13Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for miniloop-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8d6165df497b22e3606961f97b6cdf6d550df2696829ed937df8f0e7480b2216
MD5 72edafc9e44a6af46c5377f9546043de
BLAKE2b-256 fdfd76a59d49b1dcf2dfc3048cbc106d3c3acd05518e9b09c20c0232ddf93660

See more details on using hashes here.

File details

Details for the file miniloop-1.0.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: miniloop-1.0.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for miniloop-1.0.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 217bd3cfa0b99db3c1866af2e9b6cd27f4587b22dc942b1f5d6ecac3aebd71ba
MD5 d0675a4553129ec55f8bc9ee3b085af1
BLAKE2b-256 a96ffd6fca25b8ec49f04da459ed0a2c5b38fb79e531b0d84d2f91ed628baceb

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