Skip to content

Instantly share code, notes, and snippets.

@Starbuck5
Created July 18, 2025 04:32
Show Gist options
  • Select an option

  • Save Starbuck5/6bac69335b8b8145bf05a74740ef4e80 to your computer and use it in GitHub Desktop.

Select an option

Save Starbuck5/6bac69335b8b8145bf05a74740ef4e80 to your computer and use it in GitHub Desktop.
"""
Exploration of the performance of lerping between pygame-ce's C API implementation
and a pure Python implementation.
My results--
Floats
3.14 JIT lerp = 0.13 seconds
3.14 lerp = 0.15 seconds
3.14 C lerp = 0.11 seconds
3.9 C lerp = 0.10 seconds
3.9 lerp = 0.155 seconds
3.11 pypy lerp = 0.003 seconds
3.11 pypy C lerp = 0.53 seconds
Integers
3.14 JIT lerp = 0.13 seconds
3.14 lerp = 0.165 seconds
3.14 C lerp = 0.15 seconds
3.9 C lerp = 0.115 seconds
3.9 lerp = 0.20 seconds
3.11 pypy lerp = 0.004 seconds
3.11 pypy C lerp = 0.55 seconds
"""
import time
import sys
import pygame
if hasattr(sys, "_jit"):
print("JIT =", sys._jit.is_enabled())
def lerp(a: float, b: float, value: float, do_clamp: bool = True, /) -> float:
if do_clamp:
value = value if value > 0 else 0
value = value if value < 1 else 1
try:
return a + (b - a) * value
except TypeError:
raise TypeError(
"math.lerp requires all the arguments to be numbers. "
"To lerp between two vectors, please use the Vector class methods."
)
a = 27.1
b = 76.6
value = 0.3
lerp = pygame.math.lerp
print(lerp)
start = time.time()
for _ in range(1000000):
lerp(a, b, value, False)
print(time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment