/*
The result on my slow machine:
Program takes an average of 0.018000 seconds to find 78498 primes.
*/
#include <time.h>
#include <stdio.h>
#define RUNS 1000
#define SIZE 1000000
int mark[SIZE];
int main(void)
{
time_t start, finish;
int i, loop, n, num;
time(&start);
/* This loop finds the prime numbers between 2 and SIZE */
for (loop = 0; loop < RUNS; ++loop)
{
for (n = 0; n < SIZE; ++n)
mark[n] = 0;
/* This loops marks all the composite numbers with -1 */
for (num = 0, n = 2; n < SIZE; ++n)
if (!mark[n])
{
for (i = 2 * n; i < SIZE; i += n)
mark[i] = -1;
++num;
}
}
time(&finish);
printf("Program takes an average of %f seconds "
"to find %d primes.\n",
difftime(finish, start) / RUNS, num);
}
/*
The result on my slow machine:
Program takes an average of 0.018000 seconds to find 78498 primes.
*/
On 16/09/2024 06:22, Paul wrote:in just 8 seconds on a Pentium II-350"
Good prime code, comes with its own timing routines 🙂
http://cr.yp.to/primegen.html
   http://cr.yp.to/primegen/primegen-0.97.tar.gz
  Paul
I pricked my ears up when I saw this - I've been spending a quite ridiculous amount of time tuning prime code.
Happily for my sanity the program I published over on comp.lang.c++(1) is quite a bit faster than that one. On the other hand, I'm tuning for modern processors, and the comment in there say inter alia "It generates the 50847534 primes up to 1000000000
I don't remember the last time I saw one of those.
<https://en.wikipedia.org/wiki/List_of_Intel_Pentium_II_processors>
tells me it was release in 1998...
Andy
Good prime code, comes with its own timing routines 🙂
http://cr.yp.to/primegen.html
http://cr.yp.to/primegen/primegen-0.97.tar.gz
Paul
[ snip C program ]
/*
The result on my slow machine:
Program takes an average of 0.018000 seconds to find 78498 primes.
*/
Ive developed a Sieve of Erastosthenes in C++20 with an unbeaten
performance. With this code I get all primes <= 2 ^ 32 in 140ms
on my AMD 7950X 16-core Zen4-system.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 148:31:10 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,747 |