Hello.
I am quite new to c++ but am working on translating a prime generator I have made over from c#.
I have everything working well but have found that c# can output to file faster than c++ (by around 10%). This is once the 'working out' is done.
Here is the c++ code that I am currently using:
FILE * handle = fopen("primes.dat", "w");
ostringstream buffer; buffer << 2 << ' ';
for (int x = 3; x < upper_primes; x += 2)
{
if (!test_primes[x])
{
if (buffer.tellp() > 4194304)
{
fputs(buffer.str().c_str(), handle);
buffer.str("");
}
buffer << x << ' ';
}
}
fputs(buffer.str().c_str(), handle);
fclose(handle);
As you can see it outputs to file every 4MB. I have tried a few variations from 1KB up to 4MB all with similar results.
I have also tried using ofstream directly rather than ostringstream and fputs. They offer very little performance difference.
So my question is simply.. for a loop that outputs millions of numbers (originally as type int) each followed by a space can you suggest a faster way to output the list?
Once I can fine tune this I will have to work on the multi-threading for the processing. My first attempt at that made it slower (I need to keep the threads for re-using rather than creating new ones every loop) .. but thats another matter.
Thanks in advance :)
Jonathan .