Skip to main content

Benchmarked priority queue implementation in Rust

Project description

prqrs

Python priority queue in Rust via pyo3

pdm-managed license

The prqrs package provides a simple interface to a priority queue implemented in Rust. This guide will show you how to create a priority queue, add items to it, and retrieve items based on their priority.

Installation

To build the Rust project from source run:

maturin develop --release

Alternatively using the PDM package manager, just run pdm install.

Usage

Creating a Priority Queue

To create a priority queue, import the PriorityQueue class from the prqrs package and instantiate it:

from prqrs import PriorityQueue

pq = PriorityQueue()

Creating Items

Items that can be added to the priority queue need a value and a priority. Import the Item class and create items:

from prqrs import Item

item1 = Item(value=5, priority=3)  # An item with value 5 and priority 3
item2 = Item(value=10, priority=1) # An item with value 10 and priority 1

Adding Items to the Queue

Add items to the queue using the push method:

pq.push(item1)
pq.push(item2)

Retrieving Items from the Queue

Retrieve items based on their priority using the pop method. This method returns the item with the highest priority (lowest numerical value of priority):

highest_priority_item = pq.pop()
if highest_priority_item is not None:
    print(f"Value: {highest_priority_item.value}, Priority: {highest_priority_item.priority}")

Checking if the Queue is Empty

To check whether the priority queue is empty, use the is_empty method:

if pq.is_empty():
    print("The priority queue is empty.")
else:
    print("The priority queue is not empty.")

Example

Here is a complete example that demonstrates creating a priority queue, adding items, and retrieving them:

from prqrs import PriorityQueue, Item

# Create a priority queue
pq = PriorityQueue()

# Add items
pq.push(Item(value=5, priority=3))
pq.push(Item(value=10, priority=1))
pq.push(Item(value=7, priority=2))

# Retrieve and print items
while not pq.is_empty():
    item = pq.pop()
    print(f"Value: {item.value}, Priority: {item.priority}")

This will output

Value: 5, Priority: 3
Value: 7, Priority: 2
Value: 10, Priority: 1

Benchmarking

Running pdm install (or maturin develop) to install the Rust wheel followed by python benchmark_1m.py:

1.1 GHz CPU: (0.18+0.51)s

Enqueue time for 1 million items: 0.1798386573791504 seconds
Dequeue time for 1 million items: 0.5103330612182617 seconds

3.7 GHz CPU: (0.08+0.19)s

Enqueue time for 1 million items: 0.08250904083251953 seconds
Dequeue time for 1 million items: 0.1858220100402832 seconds

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

prqrs-0.1.0.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distributions

prqrs-0.1.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (655.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

prqrs-0.1.0-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.whl (663.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

prqrs-0.1.0-cp311-none-win_arm64.whl (123.3 kB view hashes)

Uploaded CPython 3.11 Windows ARM64

prqrs-0.1.0-cp311-none-win_amd64.whl (129.1 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

prqrs-0.1.0-cp311-none-win32.whl (123.6 kB view hashes)

Uploaded CPython 3.11 Windows x86

prqrs-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (655.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

prqrs-0.1.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (663.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

prqrs-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (243.0 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

prqrs-0.1.0-cp311-cp311-macosx_10_12_x86_64.whl (248.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

prqrs-0.1.0-cp310-none-win_arm64.whl (123.3 kB view hashes)

Uploaded CPython 3.10 Windows ARM64

prqrs-0.1.0-cp310-none-win_amd64.whl (129.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

prqrs-0.1.0-cp310-none-win32.whl (123.6 kB view hashes)

Uploaded CPython 3.10 Windows x86

prqrs-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (655.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

prqrs-0.1.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (663.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

Supported by

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