I'm trying to improve my code to find all the prime numbers within a set range as fast as possible, and I am trying to make it even faster. On my github page here, I have some optimization settings within Visual Studio that help improve the speed.
#include <iostream> #include <fstream> #include <Windows.h> #include <conio.h> #include <bitset> #include <string> #include <math.h> #define RUNS 10000 // How many times the code will runs. I use run each section of code a set number of times, divide by that number // to get the average. I had to to this because the program was running too fast for the GetTickCount() to get a speed. #define RANGE 100000 // What number of primes to search up to #define R2 RANGE/2 #define BRUNS 100000 int main() { //Set up varibles int count; char primes[RANGE]; int searchRange = sqrt(R2) + 1; DWORD starttime, endtime; //############################ SIEV ################################# starttime = GetTickCount(); for (int k = 0; k<RUNS; ++k) { count = 0; memset(primes, 0, R2); for (int i = 0; i < searchRange; ++i) { if (primes[i] == 0) { for (int j = (i << 1)*i + (i << 2) + (i << 1) + 3; j<R2; j += i * 4 + 6) { primes[j] = 1; primes[j + i * 2 + 3] = 1; } } } } endtime = GetTickCount(); for (int i = 0; i<R2; i++) if (primes[i] == 0) count++; float totalOp = ((float)endtime - starttime) / (1000 * RUNS); //##################################################################### //#################### MEMSET FUNCTION TIMING TEST #################### // Here I am timing how long the memset function takes, so I can take that off the time for the sieve float totalMemset = ((float)endtime - starttime) / (1000 * RUNS); //##################################################################### //#################### OUTPUT TIME AND PRIMES ###################### std::cout << "Optimised Sieve Time: "; std::cout << totalOp; std::cout << "\nPrimes Found: "; std::cout << count + 1; //##################################################################### _getch(); return 0; }
primes[i]==0? Isn't it possible that the value in memory will be0even if it's not in the address range from0toR2-1? \$\endgroup\$