Photo uplaod or Drawing

Viewed 45

Try to understand the diffrence in terms of efficency and run time in Python when i upload img or draw same image by GUI(pygame or tkinter).

Also, if i use many fonts in different locatin in my app loop . it is also reduce run speed?(comparing run with same font all the time?)

1 Answers

Also, if i use many fonts in different locatin in my app loop . it is also reduce run speed?

Avoid to create the font object in every frame. Creating a font object is a very expensive operation, because the font file needs to be read from the volume and interpreted. Create the font objects once at initialization and use them in for rendering the text:

font1 = pygame.font.SysFont(pygame.font.get_fonts()[55],23)
font2 = pygame.font.SysFont(pygame.font.get_fonts()[55],22)

font4 = pygame.font.SysFont(pygame.font.get_fonts()[8],25)
font5 = pygame.font.SysFont(pygame.font.get_fonts()[8],27)
def draw(self,health):
    # [...]

    if self.gender in ['male','Male','MALE']:
        gender = font4.render('\u2642',True,(0,153,153)) #male
        gender2 = font5.render('\u2642',True,(25,25,25))
            
    else:
        gender = font4.render(chr(0x2640),True,(255,0,0)) #female
        gender2 = font5.render(chr(0x2640),True,(25,25,25))

    # [...]
Related