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
  Program demonstrates operations of bitwise operators without using bitwise operations ShawnYang 2 494 Aug-18-2021, 03:06 PM
Last Post: deanhystad
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 1,106 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  change mouse cursor (in Windows) mgroen 0 473 Nov-11-2020, 11:26 AM
Last Post: mgroen
  Random Choice Operations Souls99 6 1,128 Jul-31-2020, 10:37 PM
Last Post: Souls99
  Create bot to automate operations in IQ Option JonatasCavalini 0 2,820 Jul-19-2020, 02:23 AM
Last Post: JonatasCavalini
  Two operations in two ranges salwa17 3 910 Jun-22-2020, 04:15 PM
Last Post: perfringo
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 1,424 Sep-17-2019, 05:21 PM
Last Post: swechsler
  Operations on indexed variables in loop Fibulavie 1 933 Aug-14-2019, 06:07 AM
Last Post: fishhook
  Confused by order of operations ward 4 1,402 Jan-22-2019, 08:53 PM
Last Post: Larz60+
  MySql Cursor field names. MuntyScruntfundle 1 1,554 Oct-22-2018, 04:10 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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