Python Forum
How to avoid slow cursor operations?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to avoid slow cursor operations?
#1
Hi. I'm using numpy as np. I have a 2D array called CC of all zeros and ones. For every one in CC, I want to change all adjacent array values to one. I want to do this recursively, and then I want to count how many steps it takes for the last zero to disappear. That is the point of the code: how far away from a one is the farthest zero. I initialized CC with
CC = np.zeros((N,N),dtype="int")  
and then I wrote some ones into it. My first idea was to make 8 copies of CC, each with the values shifted by one, (eight copies for up/down,left/right, and 4 diag) and then add those copies to CC, and then iterate. However, all the shift commands I found like roll do not shift the entire array but they send the end of each line to the beginning of the next. As it is, if CC is 1000 x 1000, then I have the below cursor doing ~eight operations for one million times. There must be a better way!
MAX_RADIUS = 0
nn = 0
CCtmp = np.zeros((N,N),dtype="int") 
while nn < 1:
    for i in range(len(CC)-1):
        for j in range(len(CC)-1):
            if i**2+ j**2 > N**2:         ## Condition for the part of CC I don't care about
                CCtmp[i,j] = 1
            if (CC[i,j] == 1 ): 
                    CCtmp[i+1,j] = 1  ## For every black pixel, color adjacent black
                    CCtmp[i,j+1] = 1  
                    CCtmp[i+1,j+1] = 1   
                    CCtmp[i-1,j] = 1
                    CCtmp[i,j-1] = 1  
                    CCtmp[i-1,j-1] = 1   
                    CCtmp[i-1,j+1] = 1   
                    CCtmp[i+1,j-1] = 1   
    CC = CC + CCtmp
    nn = np.amin(CC)
    MAX_RADIUS += 1
print('Max radius within ' + str(N) + ' is less than ' + str(MAX_RADIUS) + '.\n\n') 
Keeping mind the point of the code: how far away from a one is the farthest zero, any other suggestions are welcome.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cursor Variable inside Another Cursor . CX_ORacle paulo79 1 416 Apr-09-2022, 10:24 AM
Last Post: ibreeden
  Cursor write 3rd file empty paulo79 3 645 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE
  Program demonstrates operations of bitwise operators without using bitwise operations ShawnYang 2 1,051 Aug-18-2021, 03:06 PM
Last Post: deanhystad
  change mouse cursor (in Windows) mgroen 0 875 Nov-11-2020, 11:26 AM
Last Post: mgroen
  Random Choice Operations Souls99 6 1,674 Jul-31-2020, 10:37 PM
Last Post: Souls99
  Two operations in two ranges salwa17 3 1,248 Jun-22-2020, 04:15 PM
Last Post: perfringo
  Confused by order of operations ward 4 1,717 Jan-22-2019, 08:53 PM
Last Post: Larz60+
  MySql Cursor field names. MuntyScruntfundle 1 1,931 Oct-22-2018, 04:10 PM
Last Post: nilamo
  Best way to return Cursor Data rajuarien 0 1,451 Jul-29-2018, 09:47 PM
Last Post: rajuarien
  Python - Netezza cursor function help leojosefm 4 3,555 Jan-08-2018, 12:03 PM
Last Post: leojosefm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020