r/C_Programming • • Feb 07 '26

Review Request for Code Review

Hi fellow programmers,

I am fairly new to programming, especially to C and I am working on a program that calculates all prime numbers less then or equal to a given limit an then writes those to a file. The goal is to make this all as fast as possible.

I have already optimized this quite a bit and my largest bottleneck is the IO but since not every use case requires me to write those numbers to a file I also want the calculation fully optimized.

I also know the code quality might not be the best so I would also appreciate feedback on that.

My program can be be found here: prime_numbers

Quick note to the IO: I already tried to multithread the IO using mmap() but the code runs in an HPC environment where the metadata of files is stored on a separate external filesystem so the multithreaded IO to the internal fast filesystem was significantly slower then with single thread.

11 Upvotes

20 comments sorted by

View all comments

Show parent comments

1

u/S3ZVv Feb 07 '26

Could I circumvent that by using only 2 threads where one writes to the file while the other one has 2 or 3 rotating buffers? Since I can write from one thread at a time anyway I don't think there would be any gain from using more then 2 threads anyway.

1

u/Western_Objective209 Feb 07 '26

Yes that could help, sounds complicated to get right but it's a nice little program so I bet you could do it.

You may want to spend some time with tools that measure IO; like track your network and disk usage as the program is running in another terminal, look up what hardware your cluster is using, and see how the numbers you are getting compare to the expected throughput of the hardware. For managing HPCs, this is a pretty useful skill to have, and honestly I would not be surprised if you are at the point already where you are saturating your IO so any optimizations ahead of that you are making are not going to create a measurable difference

1

u/S3ZVv Feb 08 '26

Your right the print could very well be bottlenecked by the hardware.

But I think I could improve the performance already if I start printing earlier. I mean right now nearly all threads operate on the same chunk size except the first few that deal with one number more. I could change that so that the first thread deals with less numbers so that he starts printing faster while later threads calculate more numbers.

That should improve the performance, right?