#๐ I need help. I am doing a simulation and I want to check smth but i think it's VERY inneficient.
40 messages ยท Page 1 of 1 (latest)
@onyx zinc
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
by inefficient you mean the way used to check?(if statements)
So I've got a simulation that simulates the natural selection, some individuals are there adn evolve, they adapt to survive more. I want to check how many times have they have reproduced in their whole life. There may be in total 700 000 people to check in total, and I think doing a loop to check it one by one might be inneficient
Not necessarily, I mean the way of checking how many times has everyone reproduced, because a for loop in a list that it's length is 700 000 is very slow
Well it might not be slow, if it is not tell me pls
idk then sorry
i don't really get how you'd check that (do you store parentage or something?), so perhaps start by writing an inefficient solution and maybe you'll see how to optimize it.
maybe store everyone's relationship in a tree?
Ok so, the "individuals" are an object, and they have a atribute that is "times that I have reproduced", and every time they reproduce a 1 is added to that atribute
So what's the problem then? You can read anyone's child count straight from their attribute.
Yep ik but I won't know the average, I want to know how many times have they reproduced by average
a for loop for 700.000 elements.. where you read a single attribute in each one of them? sounds pretty fast to me, given the modern computers too
Is it? Then I will do it that way thx
You could add 1 to a global counter in addition to the personal one every reproduction. But it's a bit of a silly optimization to do prematurely; realistically just do an extra loop at the end.
I will do an extra loop
But I have another question stil
Still*
Do I have to return a list if the list is modified inside a function or not?
no you dont have to
K thx ๐
i don't think it's good practice to have global variables inside functions though
but you should think about whether it makes sense to actually do that or not
usually thats a result of bad design
doesnt have to be the case but yes usually it is
It's not like it is inside, it's just that it is modified inside
I declare the list inside the main funct and then pass it to the other functio
Is there any better and optimal way? I am thinking that a list of 700 00 elements going through functions every time might be bad
^ (oops, ping)
^^^
Ok I think I have not explained myself well enough
That list that I say it contains 700 000 objects is a list of every dead object
So therefore, at the end of the sim I will be able to go through them and divide the number of times everyone reproduced by the number of individuals
then do a loop
how so? it costs as much to pass a million-element list around as a single integer. it's not like they're getting copied, after all.
Yep you are right they are not getting copied, they are just getting like 30-40 dead individuals added every time until it gets to 700 000 individuals (more or less)
I will do it this way then, thanks ๐
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.