Hi,
> Could it be simply because Julia is compiled and Python is interpreted
Yes... and no. Compiled vs interpreted is only part of the picture. I
don't know Julia, but there are languages faster than Python.
> perhaps that SDL2 is faster than SDL1
Yes... and no. SDL2 is a LOT faster for some things that modern
projects would try to do, and a slower for others.
Having said that, try this code instead. It should get you a lot
closer to your 3 second mark :) It uses pygame.gfxdraw to draw the
boxes, which wraps the highly optimised SDL_gfx lib.
import pygame
from pygame.gfxdraw import box
import timeit
widthcell=5 # pixels
ncellswide=100
size = ncellswide*widthcell
pygame.init()
display = pygame.display.set_mode((size,size),
pygame.HWSURFACE|pygame.DOUBLEBUF)
def run():
color1=(105,105,0); color2=(0,0,150)
for i in range(500):
for row in range(ncellswide):
for col in range(ncellswide):
box(display, [row*widthcell, col*widthcell, \
widthcell-1,widthcell-1], color1)
pygame.display.update()
color1,color2=color2,color1
total_time = timeit.timeit('run()', 'from __main__ import run', number=1)
print(total_time," seconds")
pygame.quit()
The two changes are this
On 2019/06/11 21:40, David R wrote:
Is there a better place to post this question? Sorry if this isn't
the right venue.
Am I doing something wrong -- is there a way to make the following
pygame program run faster?
It runs in 10.5 seconds, whereas an equivalent program invoking SDL2
from Julia runs in 3.0 seconds (I'm happy to post the Julia code as
well.)
(I'm using pygame 1.9.4 via Anaconda in Windows 10 on a Lenovo
Thinkpad T470p.)
Could it be simply because Julia is compiled and Python is
interpreted, or perhaps that SDL2 is faster than SDL1?
import pygame
import timeit
widthcell=5 # pixels
ncellswide=100
size = ncellswide*widthcell
pygame.init()
display = pygame.display.set_mode((size,size),
pygame.HWSURFACE|pygame.DOUBLEBUF)
def run():
color1=(105,105,0); color2=(0,0,150)
for i in range(500):
for row in range(ncellswide):
for col in range(ncellswide):
pygame.draw.rect(display, color1, [row*widthcell,
col*widthcell, widthcell-1, widthcell-1])
pygame.display.update()
color1,color2=color2,color1
total_time = timeit.timeit('run()', 'from __main__ import run',
number=1)
print(total_time," seconds")
pygame.quit()