As one of my beginner C projects I'm making a simulation of a few animals. If an animal doesn't manage to get to any food within a certain amount of time, it will die.
In each frame of the game, a linked list containing pointers to these animals is traversed. First, the creatures hunger is checked, If it is too hungry, it will be removed from the list. I decided to use a linked list to store the pointers to the animals. I chose a linked list because I'm under the impression it should be pretty quick to drop an animal from it, if I'm already in that area of the list.
After the hunger check is complete, I'm planning to draw them. I have two ideas about how to do this:
A) Every animal that lives has it's location stored in an array, so I can quickly go through them and draw them (a this stage, it doesn't matter about deleting animals, they've all survived this frame, so an array should be okay?)
B) Just loop through the linked list again, drawing them.
I have a feeling option A may be faster (less dereferencing of pointers, plus the pointers to the animals will all be in continuous memory because of the array), but I'm not 100% sure. Should I just loop through the linked list to keep things simple? Or is this design just flat out retarded?
I've Google'd a lot about this, but it's not a question that is easily put into search terms. I have only been programming in C for around two weeks so please be kind.
You will probably need to implement your own vector container if you are sticking to pure C
Landon Morris
do your own homework pajeet
Nathan Thompson
Just do what's easy. Preemptively optimization is a bad idea. The linked list is likely not going to be where performance is hit unless you have many thousands of animals. Just make something, use a profiler if it doesn't meet your requirements for speed, then optimize.
Dylan Parker
figure it out faggot we don't care about your project or you if you wanna learn to program then program, dont leearn to program to act like a plebbit on Jow Forums
Julian Collins
You're approaching this from the entirely wrong angle. Why do you even care about performance at this stage? Performance is only an issue when it's an issue, i.e. when the user experiences inconvenience.
I would advise using a straightforward OO design, i.e. storing the data in each animal struct. OO is not perfect, but it was actually invented for simulations (in fact one of the first OO languages, which inspired C++, is called Simula).
But if you're really curious about performance, you are on the right track of thinking. This is problem known as "array of structs" vs. "struct of arrays". It's indeed much faster to iterate through an array. Some games actually use a pattern taking this further (and away from OO) called Entity/Component System. This is a bit an extension of your solution A.
Finally, I would advise moving to C++. You can still do C, or C-style programming in C++, but you get a lot more stuff. Proper classes, to start with.
Good luck!
Ryan Phillips
this
sit down, plan, and think out the problem instead of rushing to google or find an immediate solution
Lucas Reed
checked
also is you really want to ascend, you'd implement it as many ways as you can. first arrays, then linked-lists, then a hashmap etc.
right now you're suffering from analysis paralysis. bad habit. you should be coding.
Adam Hernandez
Just use an array and have some value as a sentinel. Like zero == dead, max_int == dead or something. Why optimize things that needn't be optimized?
Grayson Thompson
If you're really obsessed with speed then look up STB-style Stretchy Buffers and actually benchmark your result, most of your expectations will be wrong anway. Otherwise just use an array.
Kevin Taylor
I imagine a skiplist would be an excellent data structure for this assignment, but considering you are a beginner, I suggest you don't try to do anything like that. The performance of various data structures and algorithms really actually doesn't matter unless you have millions of entries.
Grayson Thomas
Does it have to be super efficient? Seems like you are overthinking it.
Sebastian Cook
>alive >dead >binary bitwise operations will be optimal
Wyatt Evans
OP. I think I have been overthinking this. Now isn’t the time for optimisation. Must just be my autism flailing up. I might even just switch th project to C++
Henry Taylor
Who will make the animal birthing simulator? Life needs balance.
Hunter Hernandez
He'd have to, for each animal, keep track of for how long it has been starving to determine that it dies.
William Butler
you're allowed to have more than one data structure :^)
Noah Wood
>Linked lists in general are really poor performing on modern hardware This is generally true. But including a bunch of periodic pointers to nodes in the list will make certain linked list operations faster than their array equivalent, especially insertion/deletion.
The general rule of thumb is that if the collection is going to be mutated alot, used linked lists with periodic pointers. In every other case, use arrays.
Jeremiah Wright
your dad's prostate
Elijah Jackson
There are more than two states of being, shitlord.
The anima, if it eats enough food, will split into two. So whilst scanning the list for dead ones, any animal which is full would cause a new node to be added to the list before the next animal is checked.
Mason Watson
don't do that at the same time. make 2 functions
Caleb Hughes
A seperate function which is called during the check loop if an animal is full, or a separate function that is called from main each frame which loops through the list in its own?
Isaac Torres
if it were me i'd have a bunch of modular functions a la unix philosophy instead of one big loop. there's many ways to do this.
Hudson Richardson
>most of your expectations will be wrong anway this