Question: optimize the below code to acheive 4 . 5 x speed #include #include #include #include #include #define N 1 0 0 0 0 0 0
optimize the below code to acheive x speed
#include
#include
#include
#include
#include
#define N The upper limit for prime generation
void sieveOfEratosthenesAVXint n
char sieve charmallocn ;
for int i ; i n; i
sievei;
int sqrtN intsqrtn;
for int p ; p sqrtN; p
if sievep
mi avxp mmsetepixcharp;
for int i p p; i n; i p
if i && i n
Use AVX to unset multiple multiples of p at once
for int j ; j ; j
mmstoreusimi&sievei jmmandsiavxpmmsetzerosi;
i ;
else
sievei;
For measuring time, we only calculate the number of prime numbers not printing them
int count ;
for int p ; p n; p
if sievep
count;
freesieve;
int main
clockt starttime clock;
printfPrime numbers up to d:
N;
sieveOfEratosthenesAVXN;
clockt endtime clock;
double executiontime doubleendtime starttime CLOCKSPERSEC;
printf
Execution time: f seconds
executiontime;
return ;
my original code:
#include
#include
#include
#include
#define N The upper limit for prime generation
void sieveOfEratosthenesint n
char sieve charmallocn ;
for int i ; i n; i
sievei;
int sqrtN intsqrtn;
for int p ; p sqrtN; p
if sievep
for int i p p; i n; i p
sievei;
for int p ; p n; p
if sievep
Print the prime numbers comment this out if you only want to measure time
printfd p;
freesieve;
int main
clockt starttime clock;
printfPrime numbers up to d:
N;
sieveOfEratosthenesN;
clockt endtime clock;
double executiontime doubleendtime starttime CLOCKSPERSEC;
printf
Execution time: f seconds
executiontime;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
