and that is the threaded c++ or the non-threaded zig? I was getting lost trying to track it by algorithm, threaded or not, etc.
your code, my box, g++ cygwin
n = 1000000
Time taken = 464 microseconds
passes in 1 sec = 2155.17
but, I believe your code cheats. The static bool is used in the condition, and after the first pass, you skip the inner loop forever.
but then it gets REALLY WEIRD.
taking the static off ar, I get this:
n = 1000000
Time taken = 112 microseconds
passes in 1 sec = 8928.57
which also looks like its keeping ar solved around.
I think you have to put bogus braces around AR so it is recreated and not be static so it is recreated and the problem actually solved each time.
also your code prints 15, 25, and many other non primes. ... so its irrelevant how fast it ran.
and taking a blunt axe to seeplus's code above, then, to see how many times it can do a million in a second...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <chrono>
#include <cmath>
template<class TimeUnit = std::chrono::nanoseconds>
class Timer1 {
public:
Timer1() {
m_start = std::chrono::steady_clock::now();
}
~Timer1() {
std::chrono::steady_clock::time_point stop = std::chrono::steady_clock::now();
std::cout << "\n** Running time: " << std::chrono::duration_cast<TimeUnit>(stop - m_start).count() << '\n';
std::cout << "\n** Running time sec: " << std::chrono::duration_cast< std::chrono::seconds>(stop - m_start).count() << '\n';
}
private:
std::chrono::steady_clock::time_point m_start;
};
int main()
{
Timer1 t;
constexpr unsigned n { 1'000'000 };
int ctr{};
auto m_start = std::chrono::steady_clock::now();
while(std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::steady_clock::now() - m_start).count() < 1000)
{
//Timer1 tm;
const auto sr { unsigned(sqrt(n)) };
bool prime[n + 1] {};
for (unsigned i { 2 }; i <= sr; ++i)
if (prime[i] == false)
for (unsigned j { i * i }; j <= n; j += i)
prime[j] = true;
ctr++;
}
std::cout <<"count per sec:"<< ctr << std::endl;
// Print all prime numbers
/*
for (unsigned p { 2 }; p <= n; ++p)
if (prime[p] == false)
std::cout << p << ' ';
*/
}
|
I get: (and the site's shell gets 90/second, wow...)
C:\c>a
count per sec:24854473
** Running time: 1000166800
** Running time sec: 1
presumably you could roughly multiply the above X number of cores for a threaded version, so my box with 20 cpus would get 10 to 20 times more (I am a little fuzzy on how the real cpu cores vs the virtual ones work).