| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
High Performance Python
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
This is the official GitHub repository for FastPython. FasterPython is an open-source project dedicated to improving Python’s performance through practical, drop-in code patterns and optimization techniques. The goal is simple: help Python run faster without sacrificing readability.
The project emphasizes the use of local variables, reduced attribute lookups, and other micro-optimizations that meaningfully improve execution speed. Each example focuses on accelerating specific functions or workloads.
FastMath Utilities is a lightweight Python module that provides fast-path wrappers around commonly used mathematical operations, constants, and performance-oriented patterns. The goal is not to replace Python’s standard library, but to expose performance-friendly access patterns that reduce overhead in tight loops, hot paths, and numerical workloads.
This module focuses on:
It is designed for Python 3.10+ and works especially well in performance-sensitive code.
Python performance is often limited not by computation itself, but by overhead:
FastMath Utilities removes these costs by:
There is no hidden magic here. The speed comes from removing friction.
This module can be vendored directly into a project or packaged as part of a larger performance-focused library.
pip install fasterPython
#Example
pip install -i https://test.pypi.org/simple/ fasterpython==0.0.8If copied directly into your codebase, no installation step is required.
Before making changes, identify the real bottlenecks using profiling tools:
import cProfile
def main():
# Your code
...
cProfile.run('main()')Thin wrappers around math functions with minimal overhead.
fast_sqrt(x)
fast_ceil(x)
fast_floor(x)
fast_factorial(x)
fast_log(x, base=None)
fast_exp(x)
fast_sin(x)Example:
from fasterPython.fasterPython import fast_sqrt, fast_log
fast_sqrt(25) # 5.0
fast_log(8, 2) # 3.0
fast_log(10) # natural logarithmConstants are exposed directly to avoid function call overhead.
FAST_PI
FAST_E
FAST_TAU
FAST_INF
FAST_NANExample:
from fasterPython.fasterPython import FAST_PI
area = FAST_PI * r * rA cached recursive Fibonacci implementation using functools.lru_cache. The lru_cache(maxsize=None) decorator in Python creates an unlimited cache that stores the results of all function calls without any eviction policy. This effectively disables the "Least Recently Used" (LRU) aspect of the cache, turning it into a simple memoization tool.
fib(n)Example:
from fasterPython.fasterPython import fib
fib(40) # computed once and reusedThis avoids the exponential blow-up of naive recursion and is suitable for repeated calls.
A hand-rolled summation loop that avoids function call overhead and shadowing built-ins.
sum_fast(iterable)Example:
from fasterPython.fasterPython import sum_fast
sum_fast([1, 2, 3, 4])This is ideal for small to medium iterables where NumPy would introduce unnecessary overhead.
Efficient square computation using local method binding to reduce attribute lookup cost.
compute_squares(nums)Example:
from fasterPython.fasterPython import compute_squares
compute_squares([1, 2, 3, 4])If NumPy is installed, a fast vectorized summation function is available.
sum_numpy(nums)Example:
from fasterPython.fasterPython import sum_numpy
sum_numpy(range(1_000_000))#Fast Bubble Sort
mylist = [64, 34, 25, 12, 22, 11, 90, 5]
n = len(mylist)
# cache the list length in a local variable
for i in range(n - 1):
# reduce the range as the largest elements bubble to the end
for j in range(n - i - 1):
a, b = mylist[j], mylist[j + 1] # local variable assignment
if a > b:
mylist[j], mylist[j + 1] = b, a
print(mylist)| Operation | Standard sort | Fast Sort | Improvement |
|---|---|---|---|
| BubbleSort | ~20 ns | ~11 ns | ~45% |
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, range(10)))| Method | Execution Time (seconds) |
|---|---|
| Normal | 0.022988299999269657 |
| Multiprocessing | 0.0000045000015234109014 |
FastMath Utilities is useful when:
It is not intended to replace NumPy, SciPy, or compiled extensions for large-scale numerical workloads.
Benchmarks were run on Python 3.12, Windows 11, x86-64 CPU, using timeit with 1,000,000 iterations unless otherwise noted. Results are representative, not absolute.
| Operation | Standard math | FastMath | Improvement |
|---|---|---|---|
| sqrt(x) | ~85 ns | ~65 ns | ~23% |
| sin(x) | ~95 ns | ~72 ns | ~24% |
The improvement comes primarily from reduced attribute lookup (math.sqrt → local binding).
| Method | Time |
|---|---|
| sum() | ~78 µs |
| sum_fast() | ~62 µs |
| sum_numpy() | ~180 µs |
For small to medium iterables, pure Python outperforms NumPy due to array construction overhead.
| Implementation | Time |
|---|---|
| Naive recursion | >10 seconds |
| Cached fib() | ~0.4 ms |
Caching converts an exponential problem into a linear one.
Python includes several built-in functions for basic mathematical operations:
min() and max(): These functions return the smallest and largest values in an iterable, respectively.
x = min(5, 10, 25)
y = max(5, 10, 25)
print(x) # Output: 5
print(y) # Output: 25abs(): This function returns the absolute (positive) value of a specified number.
x = abs(-7.25)
print(x) # Output: 7.25pow(x, y): This function returns the value of x raised to the power of y.
x = pow(4, 3)
print(x) # Output: 64The math module extends the list of mathematical functions available in Python. To use it, you must import the module:
import math
Common Functions in the Math Module
math.sqrt(x): Returns the square root of a number.
from math import sqrt x = sqrt(64) print(x) # Output: 8.0
math.ceil(x): Rounds a number upwards to its nearest integer.
from math import ceil
x = ceil(1.4)
print(x) # Output: 2math.floor(x): Rounds a number downwards to its nearest integer.
from math import floor
x = floor(1.4)
print(x) # Output: 1math.pi: Returns the value of PI (3.141592...).
from math import pi
x = pi
print(x) # Output: 3.141592653589793math.factorial(n): Returns the factorial of a number.
x = math.factorial(5) print(x) # Output: 120
math.log(x, base): Returns the logarithm of x to the given base.
from math import log x = log(100, 10) print(x) # Output: 2.0
math.exp(x): Returns e raised to the power of x.
from math import exp x = exp(2) print(x) # Output: 7.3890560989306495
math.sin(x), math.cos(x), math.tan(x): Return the sine, cosine, and tangent of x radians, respectively.
from math import sin pi x = sin(pi / 2) print(x) # Output: 1.0
math.e: The mathematical constant e (2.718281...).
from math import e x = e print(x) # Output: 2.718281828459045
math.tau: The mathematical constant τ (6.283185...), which is equal to 2π.
from math import tau x = tau print(x) # Output: 6.283185307179586
math.inf: A floating-point positive infinity.
from math import inf x = inf print(x) # Output: inf
math.nan: A floating-point "not a number" (NaN) value.
from math import nan x = nan print(x) # Output: nan
if (not a_condition) or (not another_condition):
raise exception
do_something
use while 1 instead of while True
optimization of data pipleine:
# Optimized data pipeline dataset = tf.data.Dataset.from_tensor_slices(data) dataset = dataset.cache() # Cache data in memory dataset = dataset.shuffle(buffer_size=1000) # Shuffle with an appropriate buffer dataset = dataset.batch(32) # Batch data dataset = dataset.prefetch(tf.data.AUTOTUNE) # Prefetch next batch
MIT License. Use it freely, modify it boldly, and benchmark everything.
Profile to find bottlenecks
Exploit faster built‑ins and data structures
Make loops and list operations cheaper
Improve algorithms, not just syntax
Use vectorization and compiled paths for numeric code
Environment choices
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
TBD
Distributed under the project_license. See LICENSE.txt for more information.
Thomas Yiu - @twitter_handle - tom.tyiu@gmail.com
Project Link: https://github.com/tomtyiu/fastPython
| Back | FazBrowse Home | New Git URL |