python - How can i improve this code to run faster? -


i'm making powder toy in python, , encountered problems. firstly code runs slowly. know problem in main file: http://pastebin.com/bbq4h4xu. other files detecting input / creating 2d array, problem isn't there.

within main file, problem seems in method updatescreen(). how can increase performance of function?

import pygame #inputkey.py pygame.locals import * def input_key(): global inputt inputt = "" key = pygame.key.get_pressed() if key[k_q]:     return 'q' elif key[k_w]:     return 'w' elif key[k_e]:     return 'e' elif key[k_r]:     return 'r'  #createblocks.py blocks = [] in range(400):      blocks.append([])           j in range(400):           blocks[i].append(0) 

your main.py file has print statement in loop:

def updatescreen():     #the problem here, slows down code.     in range(windh):         x in range(windw):             print x, #          <== here             if not blocks[i][x] == 0:                 if blocks[i][x] == "stone":                     screen.blit(elementstone, (x,i)) 

presumably debug? that's performing windw * windh = 2,500 print operations, slow code down sure. try removing , see how improves.


Comments