[Tutor] which of these is more efficient?
Mark Lawrence
breamoreboy at gmail.com
Mon Aug 19 05:53:52 EDT 2019
On 19/08/2019 00:55, nathan tech wrote:
> Hi there,
>
> So I am running over some coding ideas in my head for creating a map for
> a game.
>
> This map would expand based on how far the user explores.
>
> I figure there are two ways to do this:
>
> 1: the list method:
>
> map=[]
>
> for x in range(3):
>
> temp=[]
>
> for y in range(3):
>
> temp.append(default_grid_format)
>
> map.append(temp)
>
>
> then when ever the user explores a square not on the current map, it
> would do this:
>
> for x in range(len(map)):
>
> map[x].append(default_grid_format)
>
> temp=[]
>
> for x in range(len(map[0])):
>
> temp.append(default_grid_format)
>
> map.append(temp)
>
> Obviously, though, this creates a lot of data for squares that are still
> ultimately unexplored.
>
> So here was my other idea:
>
>
> 2. the dictionary method:
>
> map={}
>
> for x in range(3):
>
> for y in range(3):
>
> key=str(x)+":"+str(y)
>
> map[key]=default_grid_format
>
>
> Then when user explores new square do:
>
> key=str(player_x)+":"+str(player_y)
>
> map[key]=default_grid_format
>
>
> Is this an efficient method compared to 1?
>
> Is it, code wise, sound logic?
>
>
>
> I guess I'm just looking for a second opinion from experienced peoples.
>
> thanks everyone.
>
> Nathan
>
Quite frankly I've no idea as in 19 years of using Python my gut has
never once been correct about code efficiency, so I suggest that you try
it and see. Start with https://docs.python.org/3/library/timeit.html.
Of course this assumes that you need to do this in the first place :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list