Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize _load() #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions p5/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def _load(self):
self._size = (width, height)

data = np.array(self._img.getdata(), dtype=np.uint8)
data = np.array(self._img, dtype=np.uint8)

self._channels = len(self._img.getbands())

Expand Down
71 changes: 71 additions & 0 deletions tests/test_image_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import sys
import unittest
import threading
import types
import time
from time import perf_counter as pc

sys.path.append('../')
from p5 import *

from p5 import PImage, font, renderer
from vispy.gloo import Program

# def assertRuntime(milliseconds):
# def inner(func):
# def wrapper(self, *args, **kwargs):
# start = pc()
# func(self, *args, **kwargs)
# elapsed = int((pc() - start) * 1000)
# assert elapsed < milliseconds, f"{func.__name__} runtime exceeds {milliseconds} milliseconds. Took {elapsed}."
# return wrapper
# return inner
#
#
# class TestImageLoad(unittest.TestCase):
#
# @assertRuntime(1000)
# def test_image_load(self):
# try:
# run()
# except:
# print("shutting down")
# text("hello", (5,5))
# self.assertTrue(True)

def timer(func):
def inner(limit=None):
def wrapper(*args, **kwargs):
start = pc()
func(*args, **kwargs)
elapsed = int((pc() - start) * 1000)

if limit and elapsed > limit:
print(f"{frame_count}: WARN: {func.__name__}() exceeds {limit} ms. Took {elapsed} ms.")
else:
print(f"{frame_count}: {func.__name__}: {elapsed} ms.")
return wrapper
return inner

text = timer(text)()
font.image = timer(font.image)()

def hello():
print("hello")

setattr(PImage, "_load", timer(PImage._load)())
renderer.render_image = timer(renderer.render_image)()
setattr(Program, "draw", timer(Program.draw)())


def setup():
size(640, 480)

def draw():
background(255)
ellipse((50, 50), 100, 100)
text("hello", (50, 50))
no_loop()
exit()

run()