2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
import time
|
2014-01-06 04:45:01 +04:00
|
|
|
|
|
|
|
from PIL import PyAccess
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
from .helper import hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-12-14 21:35:04 +03:00
|
|
|
# Not running this test by default. No DOS against CI.
|
2014-01-06 04:45:01 +04:00
|
|
|
|
2014-06-25 13:19:27 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def iterate_get(size, access) -> None:
|
2014-06-25 13:19:27 +04:00
|
|
|
(w, h) = size
|
2014-01-06 04:45:01 +04:00
|
|
|
for x in range(w):
|
|
|
|
for y in range(h):
|
2014-06-25 13:19:27 +04:00
|
|
|
access[(x, y)]
|
|
|
|
|
2014-01-06 04:45:01 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def iterate_set(size, access) -> None:
|
2014-06-25 13:19:27 +04:00
|
|
|
(w, h) = size
|
2014-01-06 04:45:01 +04:00
|
|
|
for x in range(w):
|
|
|
|
for y in range(h):
|
2014-06-25 13:19:27 +04:00
|
|
|
access[(x, y)] = (x % 256, y % 256, 0)
|
|
|
|
|
2014-01-06 04:45:01 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def timer(func, label, *args) -> None:
|
2014-01-07 10:09:00 +04:00
|
|
|
iterations = 5000
|
2014-01-06 04:45:01 +04:00
|
|
|
starttime = time.time()
|
2014-01-06 20:55:06 +04:00
|
|
|
for x in range(iterations):
|
|
|
|
func(*args)
|
2019-06-13 18:53:42 +03:00
|
|
|
if time.time() - starttime > 10:
|
2014-01-06 20:55:06 +04:00
|
|
|
break
|
2023-06-22 17:16:18 +03:00
|
|
|
endtime = time.time()
|
|
|
|
print(
|
|
|
|
"{}: completed {} iterations in {:.4f}s, {:.6f}s per iteration".format(
|
|
|
|
label, x + 1, endtime - starttime, (endtime - starttime) / (x + 1.0)
|
2019-06-13 18:53:42 +03:00
|
|
|
)
|
2023-06-22 17:16:18 +03:00
|
|
|
)
|
2014-06-25 13:19:27 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_direct() -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
im = hopper()
|
|
|
|
im.load()
|
2023-06-22 17:16:18 +03:00
|
|
|
# im = Image.new("RGB", (2000, 2000), (1, 3, 2))
|
2020-03-28 04:51:28 +03:00
|
|
|
caccess = im.im.pixel_access(False)
|
|
|
|
access = PyAccess.new(im, False)
|
2014-06-25 13:19:27 +04:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
assert caccess[(0, 0)] == access[(0, 0)]
|
2014-06-25 13:19:27 +04:00
|
|
|
|
2023-11-06 21:41:21 +03:00
|
|
|
print(f"Size: {im.width}x{im.height}")
|
2020-03-28 04:51:28 +03:00
|
|
|
timer(iterate_get, "PyAccess - get", im.size, access)
|
|
|
|
timer(iterate_set, "PyAccess - set", im.size, access)
|
|
|
|
timer(iterate_get, "C-api - get", im.size, caccess)
|
|
|
|
timer(iterate_set, "C-api - set", im.size, caccess)
|