Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python plugin: point in selection?
#5
Testing if a point is in the selection appeared to be slower than desired. Then I wanted to compare the two solutions. I made the following test code:

Code:
def selected1(x,y,image): # Ofnuts
  return image.selection.get_pixel(x,y)[0] > 127

def selected2(x,y,image): # pdb
   return pdb.gimp_selection_value(image, x, y) > 127

def test(image, N):
   x = 500
   y = 500
   for i in range(N):
       test1 = selected1(x,y,image) # Ofnuts
   for i in range(N):
       test2 = selected2(x,y,image) # pdb

import cProfile
image = gimp.image_list()[0]
command = 'test(image, 100000)'
cProfile.runctx(command, None, locals(), sort='tottime')


I ran it in Gimp's Python console. The results:

Code:
        300005 function calls in 62.485 seconds

  Ordered by: internal time

  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  100000   39.456    0.000   39.456    0.000 <input>:1(selected2)
  100000   16.192    0.000   22.855    0.000 <input>:1(selected1)
  100000    6.663    0.000    6.663    0.000 {method 'get_pixel' of 'gimp.Drawable' objects}
       1    0.173    0.173   62.485   62.485 <input>:1(test)
       2    0.001    0.001    0.001    0.001 {range}
       1    0.000    0.000   62.485   62.485 <string>:1(<module>)
       1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


So, using a call to pdb (pdb.gimp_selection_value(image, x, y)) takes about 73% more time than your method (image.selection.get_pixel(x,y)[0]). The numbers are 39.456 and 22.855. There must be an explanation?

(Note: I did this on an old and slow machine.)
Reply


Messages In This Thread
RE: Python plugin: point in selection? - by Ottia Tuota - 01-25-2021, 12:10 PM

Forum Jump: