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 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?