pgzero icon indicating copy to clipboard operation
pgzero copied to clipboard

Remove dependency on numpy.

Open lgautier opened this issue 3 years ago • 2 comments

Make gradients between 4 and 5 times faster while at it.

This is a proposed partial fix for issue #270. Current version of ptext used still requires numpy.

lgautier avatar Jan 09 '22 02:01 lgautier

I'm surprised that smoothscale is faster; each pixel will be blended between 4 values rather than just copied. So if this is faster then it should be possible to go faster still...

lordmauve avatar Jan 09 '22 16:01 lordmauve

The (code to run the) benchmark is in a comment for issue #270. Beside numpy not being as fast as one would expect, possible reasons for increased performance are:

  • the averaging is something the implementation in the backend could delegate to hardware (the chain is pygame zero -> pygame -> SDL).
  • gradient created in-place (no need to allocate an array as large as the screen)

An alternative I have considered is to create a first column of pixels (one pixel per row) and scale across the width. That approach would be better if considering stripes in gradients that are several pixels tall. With a bit of attention this could also help make partial gradients (e.g., sunset or sunrise skies, transition between a planet's atmosphere and space). However, for this to be most useful there would be the need to change the API in pygame zero. Since using scalesmooth was already so much faster that the current implementation, and achieved my goal of removing a strict dependency on numpy. I did not bother experimenting much further.

lgautier avatar Jan 09 '22 18:01 lgautier