giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.08k stars 1.37k forks source link

[NetBSD] speedup net_connections() with proper filtering in C #2342

Closed giampaolo closed 6 months ago

giampaolo commented 6 months ago

Summary

Description

Modify psutil.net_connections() to retrieving unnecessary connection types unless explicitly asked. E.g., on an IDLE system with few IPv6 connections this will run around 170% faster:

import psutil, time
started = time.monotonic()
for x in range(1000):
    psutil.net_connections("tcp6")
print(f"completed in {(time.monotonic() - started):.4f} secs")

Before all connection types (TCP, UDP, UNIX) were retrived internally, even if they were not returned.