Code running slow on certain function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* THIS IS MY PROJECT:
Write a program that creates a linked list of bunny objects.																							
Each bunny object must have																																
gender: Male, Female (random at creation 50/50)																											
color: white, brown, black, spotted																														
age : 0-10 (years old)																																	
Name : randomly chosen at creation from a list of bunny names.																							
radioactive_mutant_vampire_bunny: Bunny(95% Chance), Mutant(5% Chance)																					

At program initialization 5 bunnies must be created and given random colors.																			
Each turn afterwards the bunnies age 1 year.																										
So long as there is at least one male age 2 or older, for each female bunny in the list age 2 or older;
a new bunny is created each turn. (i.e. if there was 1 adult male and 3 adult female bunnies, three new bunnies would be born each turn)
New bunnies born should be the same color as their mother.
If a bunny becomes older than 10 years old, it dies.
If a radioactive mutant vampire bunny is born then each turn it will change exactly one non radioactive bunny into a radioactive vampire bunny.
(if there are two radioactive mutant vampire bunnies two bunnies will be changed each turn and so on...)
Radioactive vampire bunnies are excluded from regular breeding and do not count as adult bunnies.
Radioactive vampire bunnies do not die until they reach age 50.
The program should print a list of all the bunnies in the colony each turn along w/ all the bunnies details, sorted by age.
The program should also output each turns events such as
"Bunny Thumper was born!
Bunny Fufu was born!
Radioactive Mutant Vampire Bunny Darth Maul was born!
Bunny Julius Caesar died!
The program should write all screen output to a file.
When all the bunnies have died the program terminates.
If the bunny population exceeds 1000 a food shortage must occur killing exactly half of the bunnies (randomly chosen)
*/


The code I am running is "colony.radiate()" object, it essentially gets all mutants in the colony, and infects 1 non mutant bunny for each mutant there is and outputs that the bunny got mutated. However whenever I run the function, and there are about 1000 bunnys in the colony, it takes about 3 seconds to get one output.

main.cpp
1
2
3
4
5
6
7
8
9
10
int main() {
	Colony main_colony = Colony::Colony({});
	
        .................................

	main_colony.radiate();
        ................................

	return 0;
}


Colony.cpp Radiate()
1
2
3
4
5
6
7
8
9
10
11
12
13
void Colony::radiate() {
	std::vector <Bunny> mutant_colony;

	for (int i = 0; i < colony.size(); i++) {
		if (Creature::isMutant(Creature::toType(colony[i].getCreature()))) {
			mutant_colony.push_back(colony[i]);
		}
	}

	for (int i = 0; i < mutant_colony.size(); i++) {
		infectRandom();
	}
}


Colony.cpp infectRandom();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void Colony::infectRandom() {
	std::vector <Bunny> bunny_colony;

	for (int i = 0; i < colony.size(); i++) {
		if (colony[i].getCreature() == "Bunny") {
			bunny_colony.push_back(colony[i]);
		}
	}

	if (bunny_colony.size() == 0) {
		return;
	}

	std::random_device rd;
	std::mt19937 gen(rd());
	std::uniform_int_distribution<> dist(0, bunny_colony.size() - 1);

	int random_number = dist(gen);

	Game::announce(bunny_colony[random_number], "is now a mutant!");
	bunny_colony[random_number].setCreature("Mutant");

	for (int i = 0; i < colony.size(); i++) {
		if (colony[i].getCreature() == "Bunny") {
			colony[i] = bunny_colony[0];
			bunny_colony.erase(bunny_colony.begin());
		}
	}
}


Note: The max amount of bunnys a colony can have is 1000, but that check doesnt happen till the end of the turn so theoretically the maximum possible amount of bunnies is 2000 in a colony.
1
2
3
4

	std::random_device rd;
	std::mt19937 gen(rd());
	std::uniform_int_distribution<> dist(0, bunny_colony.size() - 1);

Try doing this just once in main, or some outer function, then pass a reference to it.
salem is right. std::random_device is slow. Seeding std::mt19937 is slow. However, std::uniform_int_distribution is fast so no harm in creating it inside the function whenever you need it.
Note: The max amount of bunnys a colony can have is 1000, but that check doesnt happen till the end of the turn so theoretically the maximum possible amount of bunnies is 2000 in a colony.

then theoretically the loop at 23 can do up to 2x the amount of work required. It isn't likely a cause, but depending on how often that happens, it could be a trouble spot.
Last edited on
Topic archived. No new replies allowed.