Compare commits

...

5 Commits

Author SHA1 Message Date
Stedd de3c5f9f08 edited performance printout 2024-12-01 17:29:20 +01:00
Stedd 6a23fa268e Small gain in performance from reserving space
I don't want to hardcode the number of lines in the input file.
2024-12-01 17:29:02 +01:00
Stedd 3d44c7c416 Using the filestream to run the parser loop directly
Also removed col2 variable and calculating occurrence_map during parsing.
2024-12-01 17:28:18 +01:00
Stedd 89318eaf4c added stress testing 2024-12-01 16:52:23 +01:00
Stedd ba00e02c3a The if is not actually needed, I redundantly created the standard behaviour of unordered_map 2024-12-01 16:37:06 +01:00
2 changed files with 30 additions and 25 deletions

View File

@ -153,10 +153,13 @@ Once again consider your left and right lists. What is their similarity score?
void day01::Part2() void day01::Part2()
{ {
std::vector<int> col1; std::vector<int> col1;
std::vector<int> col2;
std::unordered_map<int, int> occurrence_map; std::unordered_map<int, int> occurrence_map;
int sum = 0; int sum = 0;
size_t estimated_size = 1000;
col1.reserve(estimated_size);
occurrence_map.reserve(estimated_size);
std::ifstream input("input/day01.txt"); std::ifstream input("input/day01.txt");
//std::ifstream input("input/day01short.txt"); //std::ifstream input("input/day01short.txt");
@ -168,31 +171,17 @@ void day01::Part2()
return; return;
} }
std::string delimiter = " ";
auto delimiterLength = delimiter.length();
std::string line; std::string line;
while (std::getline(input, line)) int num1, num2;
while (input >> num1 >> num2)
{ {
auto delimiterPos = line.find(delimiter); col1.emplace_back(num1);
col1.emplace_back(std::stoi(line.substr(0, delimiterPos))); occurrence_map[num2]++;
col2.emplace_back(std::stoi(line.substr(delimiterPos + delimiterLength, line.length())));
}
for (auto number: col2)
{
if (occurrence_map.contains(number))
{
occurrence_map[number]++;
} else
{
occurrence_map[number] = 1;
}
} }
for (auto number: col1) for (auto number: col1)
{ {
sum += number * occurrence_map[number]; sum += number * occurrence_map[number];
} }
printf("Sum of all occurrence pairs is: %d\n", sum); //printf("Sum of all occurrence pairs is: %d\n", sum);
} }

View File

@ -5,9 +5,25 @@
int main() int main()
{ {
const auto startTime = std::chrono::high_resolution_clock::now(); double lowest = 10000;
day01::Part2(); double totalTime = 0;
const auto endTime = std::chrono::high_resolution_clock::now(); constexpr int cycles = 10000;
const std::chrono::duration<double> diff = endTime - startTime; for (int i = 1; i <= cycles; i++)
std::cout << "Execution time: " << diff.count() * 1e6 << " μs" << std::endl; {
const auto startTime = std::chrono::high_resolution_clock::now();
day01::Part2();
const auto endTime = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double> diff = endTime - startTime;
totalTime += diff.count();
if (diff.count() < lowest)
{
lowest = diff.count();
}
}
const double average = totalTime / cycles;
std::cout << "Ran " << cycles << " cycles. "
<< "Execution time avg: " << average * 1e6 << " μs. Lowest: "
<< lowest
* 1e6 << " μs" << std::endl;
} }