Pillow/Tests/bench_cffi_access.py

59 lines
1.5 KiB
Python
Raw Normal View History

import time
from PIL import PyAccess
2020-03-28 04:51:28 +03:00
from .helper import hopper
# Not running this test by default. No DOS against Travis CI.
def iterate_get(size, access):
(w, h) = size
for x in range(w):
for y in range(h):
access[(x, y)]
def iterate_set(size, access):
(w, h) = size
for x in range(w):
for y in range(h):
access[(x, y)] = (x % 256, y % 256, 0)
def timer(func, label, *args):
2014-01-07 10:09:00 +04:00
iterations = 5000
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:
print(
"{}: breaking at {} iterations, {:.6f} per iteration".format(
label, x + 1, (time.time() - starttime) / (x + 1.0)
)
2019-06-13 18:53:42 +03:00
)
2014-01-06 20:55:06 +04:00
break
2019-06-13 18:53:42 +03:00
if x == iterations - 1:
2014-01-06 20:55:06 +04:00
endtime = time.time()
2019-06-13 18:53:42 +03:00
print(
"{}: {:.4f} s {:.6f} per iteration".format(
label, endtime - starttime, (endtime - starttime) / (x + 1.0)
)
2019-06-13 18:53:42 +03:00
)
2020-03-28 04:51:28 +03:00
def test_direct():
im = hopper()
im.load()
# im = Image.new( "RGB", (2000, 2000), (1, 3, 2))
caccess = im.im.pixel_access(False)
access = PyAccess.new(im, False)
2020-03-28 04:51:28 +03:00
assert caccess[(0, 0)] == access[(0, 0)]
2020-03-28 04:51:28 +03:00
print("Size: %sx%s" % im.size)
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)